Reputation: 4587
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.
Upvotes: 18
Views: 20082
Reputation: 41533
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);
Upvotes: 5
Reputation: 72858
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);
Upvotes: 28