2

I'm trying to make a simple viewmodel to display a list of objects with Knockback. The View Model is rather simple, looking like this:

var objectives_collection = new ObjectiveCollection();
objectives_collection.fetch();

var view_model = kb.ViewModel.extend({
    objectives: kb.CollectionObservable(objectives_collection),
    constructor: function() {
        _this = this;
        kb.ViewModel.prototype.constructor.call(this, model = new Objective(), {});
        console.log(this.objectives);
        return this;
    },
    addObjective : function() {
        var objective_model = new Objective({name: "New Objective", descriptor: 'Add description here'});
        this.objectives.add(objective_model);
        objective_model.save();
        console.log(objectives);
    },
    edit: function() {
        console.log('Edit');
    },
    remove: function(objective) {
        self.objectives.remove(objective);
    }

});

Right now though, the first line of the view model (kb.CollectionObservable(objectives_collection)) is giving me grief. It's calling Underscore.js' bind function and trying to set up the onCollectionChange listener, but Knockback's line here is trying to bind an undefined function:

this.__kb._onCollectionChange = _.bind(this._onCollectionChange, this);

where this._onCollectionChange is apparently undefined. I'm not really sure what to make of this; what am I doing wrong here?

moberemk
  • 1,597
  • 1
  • 18
  • 39
  • 1
    Didnt you just forget the `new`? `this` should be the global object `window` in your `kb.CollectionObservable(objectives_collection)` call. I may be wrong though, don't know what it really is so can't tell for sure. – Loamhoof May 27 '13 at 16:00
  • None of the examples I've seen included a new command though; while adding it DOES stop the error from being thrown, it then makes it such that the attached collection is a new, empty collection which I can't affect. – moberemk May 27 '13 at 17:10
  • 1
    I don't know how Kb works so I can't help you more. But your first problem was a context problem due to the fact that you didn't use `new`. – Loamhoof May 27 '13 at 18:08
  • Oh, you were plenty of help--adding the new did fix my issue. It's just odd to me because none of the online examples seemed to make that call, which is why I found it unusual. – moberemk May 27 '13 at 18:22

1 Answers1

1

There are two functions in Knockback.js for creating a CollectionObservable.

One function is a constructor called kb.CollectionObservable, another is a factory method called kb.collectionObservable. Notice the case-sensitive difference?

When you use the constructor, you call it as:

var myCollection = new kb.CollectionObservable(...);

With the factory, you're calling a method which uses the new keyword for you. So you would type:

var myCollection = kb.collectionObservable(...);

Hope that clears it up.

Trevor Elliott
  • 11,292
  • 11
  • 63
  • 102
  • Yeah, I figured this out weeks ago. Still appreciate the answer though! – moberemk Jul 17 '13 at 17:24
  • I was searching for the answer myself. When you find the answer you should answer your own question so that people find the solution when they search stackoverflow for the same question. A single answer can help hundreds of people, not just the asker. – Trevor Elliott Jul 17 '13 at 17:31
  • Actually, I was waiting for Loamhoof to post his answer as an answer so I could give him the karma for helping him out--you can see his stuff in the comments above. – moberemk Jul 17 '13 at 18:30