Thursday, 15 April 2010

backbone.js - Persisting a backbone model and the id -



backbone.js - Persisting a backbone model and the id -

let's assume create new model class instanciate person using it:

var user = backbone.model.extend({ urlroot: '/user' }); var nou = new user({ name: "nourdine" });

now, of course, want persist it. not having added id backbone create post request , communicate server intention create entity under /user containing info {name: "nourdine"}. here's how it:

nou.save(null, { success: function (model, response, options) { // ... do here? } })

the server create record in db containing json info rearranged in form , assign id it. now:

1 - server suppose homecoming in http response? json containing json provided clinet + newly created fields, namely id of new record?

2 - going update model in client these data? me? matter of fact tell model in client new id has been assign server next time user.save() obtain set rather post. supposed update model in client?

thanks

so work flow this

client -> create model , populate info client -> save model (model.save()) server -> create server side version of model using data, assign id server -> respond success , id of newly created model client -> in success set id 1 passed

now potential issue have work flow if did not set in server model still created, client model not reflect of server anymore, minimize returning error if model not created passed.

and able phone call model.save() 1 time again time having id initiating set request

backbone.js

No comments:

Post a Comment