javascript - Location of the parse attribute on Ampersand.js model or collection -


on current project, using ampersand.js models , rest-collections. when wire api running trouble. api returns object this...

{      type: ...,     multi: ...,     data: <good stuff> } 

in order load data model or collection understanding need use parse.

after going on docs seems should place parse in model.

in practice though, when run fetch against collection, not load data models unless parse property on collection. however, when run getorfetch not load data unless parse property on model.

nothing works if put parse in both model , collection.

it doesn't make sense should have move it. need know suppose live, , need work.

here model , collection:

var case = model.extend({     ajaxconfig: function () {         return {             headers: {                 'x-auth-token': 'testing'             }         };     },     parse: function (response) {         return response.data;     },     props: {         id: 'string',         orgid: 'string',         created: 'string',         lastupdated: 'string',      } });  var casecollection = restcollection.extend({     model: case,     url: '/cases',     ajaxconfig: function () {         return {             headers: {                 'x-auth-token': 'testing'             }         };     },     parse: function (response) {         return response.data;     },     getcase: function (caseid, callbackfunc) {         this.getorfetch(caseid, function (err, model) {             if (err) {                 console.log(err);             } else {                 callbackfunc(model.tojson());             }         });     },     getcases: function (callbackfunc) {         this.fetch({             success: function (collection, response) {                 callbackfunc(collection.tojson());             }         });     } }); 

well, depends on data structure of resource;

if expected data array of objects, i.e. client requests list, should override collection's parse. if resource responds object, i.e. client requests single item, you'd override model's parse.

as per case - resource returns list of objects, override collection's parse traverse response data , return relevant value.

provided objects in response array matching model's structure, should not override model's parse not meddle model's construction.

ampersand's getorfetch should not affected this, it's caching existing data, or fetching model expected resource.

the problem in server's (rest) api. backbone derives model's resource collection's url, appending model's id url segment, unless backend doesn't serve @ /cases/{id}, fetch on model tied collection fail.

to solve this, either change model's url overriding rooturl, or create such resource in backend.

from backbone docs on model.url:

returns relative url model's resource located on server. if models located somewhere else, override method correct logic. generates urls of form: "[collection.url]/[id]" default, may override specifying explicit urlroot if model's collection shouldn't taken account.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -