0

There are a number of questions like this. I have not found an answer.

Using the example TodoMVC with backbone.js and require.js, I want to fetch from the server not from localstorage.

I have an url that return a proper json collection, where the models is like this:

{"string1": "foo", "string2":"bar", "somefloat":0}

In my model (model/todo.js) i change defaults to:

defaults: {string1: '', string2: '',somefloat: 0},

and in my collection (collections/todos.js) i comment out localstorage and add in a url.

This makes the fetch go to my server, and i can see that it returns the json collection.

But for some reason the model is undefined in backbone.js line 817

    // Prepare a model or hash of attributes to be added to this collection.
    _prepareModel: function (model, options) {
        options || (options = {});
        if (!(model instanceof Model)) {
            console.log(Model);
            var attrs = model;
            console.log(attrs.Kana);
            options.collection = this;

            //ERROR IN THIS LINE: Uncaught TypeError: undefined is not a function 
            model = new this.model(attrs, options);

            if (!model._validate(model.attributes, options)) model = false;
        } else if (!model.collection) {
            model.collection = this;
        }
        return model;
    },

Why is the model undefined when I'm not using localstorage?

PrestaShopDeveloper
  • 3,110
  • 3
  • 21
  • 30
Carsten
  • 5
  • 1
  • Did you also comment `'lib/backbone/localstorage',` in the function's define of your collection? Because for what I know Backbone Localstorage just replace the default Backbone.Sync() function and so if you try to use the classic ajax method to fetch data it will not work. – Ingro Oct 04 '12 at 12:30
  • Thanks for your reply. In collections/todos I commented out `'lib/backbone/localstorage'`," and `localStorage: new Store('todos-backbone'),` and then i inserted `url: '/Practice/GetCollection',`. I also tried adding `initialize: function(){this.model = Todo;},` to the collection. Didn't make a difference. – Carsten Oct 04 '12 at 12:52
  • Do you added the `urlRoot` property to the Model? It should be: `urlRoot:'/Practice/GetCollection'` – Ingro Oct 04 '12 at 12:58
  • No I didn't. And it worked :o) I wonder why I need to use it. The api says something like "use urlRoot if you are using a model outside of a collection". I hope it will make more sense once i get to work some more with the todoMVC example. Thanks a lot Ingro. – Carsten Oct 04 '12 at 13:14
  • You're welcome! Honestly I don't know why it's needed (as you said the documentation didn't mention it) but I had the same problem when I started to work with Backbone so... With Backbone, in the beginning, some things are not very intuitive but once you get used to them you will like it. I'm going to add the Answer aswell! – Ingro Oct 04 '12 at 13:25
  • If the `urlRoot` is working and `url` isn't, it's usually an indicator that your model is (at that point) NOT in a collection. You might do this when you create a model first with id, to fetch the model data then add to some collection after it's fetched. If you create the model and `add()` it to a collection first, the `model.fetch()` looks for the collection.url definition as its goto address. Thus, I'm not sure it's the most "semantically?" correct way to do a `urlRoot` of `/Practice/Collection`. Usually a model urlRoot will be something like `/model` and Backbone appends an `/:id` to it. – jmk2142 Oct 05 '12 at 02:53

1 Answers1

0

You also need to set the property urlRoot in you Model:

var TodoModel = Backbone.Model.extend({

    urlRoot: "/Practice/GetCollection",
Ingro
  • 2,841
  • 5
  • 26
  • 42
  • Unfortunately, It didn't work. The problem persists. I don't know what other information I could give. :-( – Carsten Oct 04 '12 at 20:05
  • OK ok. My bad. I'm going to accept Ingros solution, and add the comment that (obviously) you should also remove Store from the parameter list on the collection. – Carsten Oct 05 '12 at 10:49
  • I'm glad that you find the solution :) – Ingro Oct 05 '12 at 12:43