18

I know how to create a new backbone model. But how I can create a backbone model with the data which is received from a web service?

For example, you are receiving a JSON data from a webservice. I want to use this JSON as backbone model. How I can do that?

Thanks.

2 Answers 2

28

MyModel = Backbone.Model.extend({});

var data = { /* some data you got from the ajax call */};

var m = new MyModel(data);

Or if you don't need a specific type of model, you can just use a generic Backbone.Model


var data = { /* some data you got from the ajax call */};

var m = new Backbone.Model(data);
Sign up to request clarification or add additional context in comments.

Comments

5

It's not clear if you're trying to create a model definition or a model instance.
Either way, if your service is returning a json object, somehing like should work:

var data = {/*received data*/};

// for a new model definition
var newModelDefinition = Backbone.Model.extend(data);
// that you can instantiate later on:
var model1 = new newModelDefinition(),
    model2 = new newModelDefinition(someData);

// for a new model instance
var newModelInstance = new Backbone.Model(data);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.