1

I am running into this issue with backbone where the model seems to be undefined to backbone, though all scripts are loaded. (I am using require to load backbone and other javascript files).

So whenever I do a collection.fetch I get this error in firebug:

TypeError: targetModel is undefined

When I run the script it holds at this point:

if (attrs instanceof Model) {
    id = model = attrs;
} else {
    id = attrs[targetModel.prototype.idAttribute];
} 

when I hover with my mouse over targetModel it says: undefined It somehow doesn't seem to work now and the only thing I did was changing my html template, which only get loaded after the collection.fetch.

Can you please help me out here?

Here is my model:

var OF = OF || {};

OF.UsersMdl = Backbone.Model.extend({

    default: {

        username: '',
        mailinglist: '',
        email: ''

    },

    initialize: function() {

        //

    },

    result: {
        success: false,
        message: ''
    },

    validate: function(att) {

    }

});

Here is the collection:

var OF = OF || {};

OF.UsersCollection = Backbone.Collection.extend({

    initialize: function() {
        //
    },

   parse: function(data){
        return data["all-users"];
    },

    model: OF.UsersMdl,

    url: 'php/api/users'

});

And last but not least the router with the require part:

goToUsers: function() {

    require(['./models/users', './views/users_view', './collections/user_collection'], function(UsersMdl, UsersView, UsersCollection) {

        OF.usersMdl = new OF.UsersMdl;
        OF.usersCollection = new OF.UsersCollection;
        OF.usersView = new OF.UsersView;

        //when the collection is fetched
        $.when(OF.usersCollection.fetch({
            data: {
                "admin": OF.login.attributes.admin,
                "session": OF.login.attributes.session
            },
            success: function(){
                //console.log(OF.usersCollection.length);
            }

        //then render the view
        })).then(function(){

            OF.usersView.render();
        }, 300);

    });

},

Here is the JSON which will be retreived by the fetch:

{ "all-users": [ { "username":"tester", "mailinglist":"1", "email":"tester@tester.test" }, { "username":"tester2", "mailinglist":"1", "email":"tester2@tester.test" }, { "username":"tester3", "mailinglist":"0", "email":"tester3@tester.test" } ] }

Thanks in advance

BonifatiusK
  • 2,281
  • 5
  • 29
  • 43
  • 1
    Where is `targetModel` defined? – fbynite Dec 16 '13 at 22:00
  • targetModel is a backbone variable. So I do not define this. Unless you mean the "model: OF.UsersMdl," part in the collection? – BonifatiusK Dec 17 '13 at 06:58
  • I personally never heard of 'targetModel' default variable, could you post a link to documentation, where it is described? – Marian Polacek Dec 17 '13 at 07:20
  • Have a look at backbone-1.1.0.js at row 669 it's just an internal of the Backbone collection: the model is defined in the _.extend(Collection.prototype, Events, { model: Model – BonifatiusK Dec 17 '13 at 07:27
  • I see this is new to backbone1.1.0 in Backbone 1.0.0 the 'targetModel' was this: var model = new this.model(attrs, options); However this is still undefined for me, so It must me something with calling the model in the collection. – BonifatiusK Dec 17 '13 at 07:33
  • can you put this code in a jsfiddle? – ekeren Dec 17 '13 at 09:11
  • Can you add the json code that gets fetched 'php/api/users' – ekeren Dec 17 '13 at 09:14
  • here is a fiddle but I don't know how to get the API done there: http://jsfiddle.net/Uv6R3/3/ – BonifatiusK Dec 17 '13 at 11:09
  • When you change the template back it works? – ekeren Dec 17 '13 at 12:37
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/43328/discussion-between-ekeren-and-bonifatiusk) – ekeren Dec 17 '13 at 12:49

1 Answers1

6

I had this same error and banged my head against it for quite a while because backbone is new to me and this was compounding a fetch issue. Anyhow, I eventually figured out that order matters. Doh! (Less obvious when using CoffeeScript and "class" statements I thinks.) With one of my models I was setting the Collection before the Model (thanks to bad example code from the Backbone.js on Rails book). I reversed that and this error went away to reveal my true fetch issue.

Similarly, your model: property may be invalid for this reason or another reason, leaving it undefined when attempting to reference later.

Side note: I had a similar error in Backbone 1.0.0. When I upgraded to Backbone 1.1.0 I then got this exact error at the same point in backbone code.

juanitogan
  • 1,698
  • 1
  • 22
  • 37
  • 1
    Setting the collection before my model was in fact my problem, thanks for the tip! definitely helped me out :D – AzurGroup Mar 13 '14 at 16:18