I have been fighting with this problem for hours. I can't seem to figure out why my code had worked and now is not!
I'm building a fairly plex application with Backbone and Marionette on requirejs and I'm new to all of these libraries so it is a little confusing.
I'm getting the error
Uncaught TypeError: Object [object Object] has no method 'apply'
And I can't seem to figure out what happened or why my app does not work now no matter what code I seem to ment out.
ANY help to point me in the right direction?
I have been fighting with this problem for hours. I can't seem to figure out why my code had worked and now is not!
I'm building a fairly plex application with Backbone and Marionette on requirejs and I'm new to all of these libraries so it is a little confusing.
I'm getting the error
Uncaught TypeError: Object [object Object] has no method 'apply'
And I can't seem to figure out what happened or why my app does not work now no matter what code I seem to ment out.
ANY help to point me in the right direction?
Share Improve this question asked Jan 17, 2013 at 5:13 rkstarrkstar 1,23015 silver badges27 bronze badges1 Answer
Reset to default 7I found the problem. I had been going through my code and "optimizing" (so I thought) some of the redundancies.
Instead of this:
define(["backbone"], function( Backbone ){
var model = Backbone.Model.extend({
myfunc : function(){ /*do stuff */}
});
return model;
});
I had changed a bunch of code to look like this:
define(["backbone"], function( Backbone ){
return Backbone.Model.extend({
myfunc : function(){ /*do stuff*/ }
});
});
Not a problem right? Mostly.
I was ALSO using a nifty trick I read about on another post about getting singleton functionality from require.js modules by returning an instantiated model! Cool... mostly.
I had this:
define(["backbone"], function( Backbone ){
var singleton = Backbone.Model.extend({
myfunc : function(){ /*do singleton stuff*/ }
});
// because require.js only runs this code once, this essentially creates a singleton!
return new singleton();
});
and it worked great!
Then I got cocky...
define(["backbone"], function( Backbone ){
return new Backbone.Model.extend({
myfunc : function(){ /*do singleton stuff... no more!*/ }
})();
});
and I started getting my has no method 'apply'
error.
It took me the better part of 2 days and lots of frustration to track down the problem and I finally got the answer from this post:
new Backbone.Model() vs Backbone.Model.extend()
It explains nicely how you can instantiate Backbone.Model but Backbone.Model.extend merely provides you with a constructor that you can THEN use to instantiate a custom Backbone.Model object.
I hope this saves someone the pain I just went through.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745063757a4609126.html
评论列表(0条)