javascript - How to call methods inside of a Backbone Collection -
i'm new backbone.js, new javascript, not sure how put question in phrase, want function called , returns result attribute in same collection.
var enrollmentcollection = backbone.collection.extend({ url: "/some/prefix" + this.getpath() + "/some/suffix", getpath: function() { var result; $.ajax({ type: 'get', url: '/this/is/a/url', async: false, datatype: 'json', success: function(data) { result = data[0].someattribute; } }); return result; } }); as can see, getpath() method should returning value value of url, however, browser gives following error:
uncaught typeerror: cannot read property 'getpath' of undefined(anonymous function)
any appreciated.
with code using:
url: this.getpath(), you executing function this.getpath() , doesn't exist yet because defined below.
try doing
url: (function(){ var result; $.ajax({ type: 'get', url: '/this/is/a/url', async: false, datatype: 'json', success: function(data) { result = data[0].someattribute; } }); return result; }())
Comments
Post a Comment