javascript - Backbone.Model => Uncaught TypeError: Object [Object object] has not method 'apply' - Stack

I have been fighting with this problem for hours.I can't seem to figure out why my code had work

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 badges
Add a ment  | 

1 Answer 1

Reset to default 7

I 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信