ember.js - Get server's answer after saving model -
i use ember data node js server; model looks simple:
gmcontrolpanel.product = ds.model.extend({ name: ds.attr('string'), description: ds.attr('string'), });
once node server receives product.save(), persists record in mysql db, managing record id , answers this:
{ product { id: 1, name: "aaa", description "bbb" } }
i need id of server's response (not promise returned save().then(), id null); how can it?
update:
the node server, using express:
gmserver.post('/products', function (req, res) { rootname = "product"; querystring = 'insert products (id, name, descriptions ) values ( '+ counters.prodcuts +', "' + req.body.product.name + '", "' + req.body.product.description + '")'; executequery(req, res, querystring); responsetopost(counters.products, req.body.product, rootname, res); counters.products++; }); function executequery (req, res, querystring) { connection.query(querystring, function(err, rows, fields){ if (err) throw err; }); } function responsetopost (id, data, rootname, res) { var result = new object(); result[rootname] = new object(); var = 0; var answer; result[rootname].id = id; for(var key in data) { result[rootname][key] = data[key]; } answer = json.stringify(result, null, '\t'); console.log(answer); res.send(answer); }
i can see log of answer here, answer 1 written above; tried change responsetopost send static value this:
result[rootname][key] = 'aaa';
but in ember, doing
product.save().then(function(savedproduct) { console.log(savedproduct.get('name')); }
i sumbmitted value of name, not 'aaa' expected...
second update: doing in ember
product.save().then(function(savedproduct) { console.log(savedproduct); }
to see savedproduct is, in chrome see result of log:
class {id: null, store: class, container: container, currentstate: (...), errors: class…} __ember1395755543625: "ember548" __ember1395755543625_meta: object __nextsuper: undefined _attributes: object _changestosync: object _data: object __ember1395755543625_meta: meta _super: function superfunction(){ name: "asdf" description: "asdfa" __proto__: object _deferredtriggers: array[0] _inflightattributes: object _relationships: object _suspendedrelationships: false _updatingrecordarrayslater: false container: container currentstate: (...) currentstate: function () { set currentstate: function (value) { data: (...) errors: class id: null iserror: false store: class tostring: function () { return ret; } __proto__: object
where "asdf" , "asdfa" values typed in insert form on app
the record should updated if that's json returned.
product.save().then(function(record){ //record same product here console.log(record.get('id')); });
Comments
Post a Comment