mbest / knockout-deferred-updates

Deferred Updates plugin for Knockout <=3.3.0
http://mbest.github.io/knockout-deferred-updates/
134 stars 23 forks source link

dependentObservable.notifySubscribers = undefined #15

Closed DrSammyD closed 10 years ago

DrSammyD commented 10 years ago

I'm having an issue on line 535 where the dependentObservable says it has no method notifySubscribers. I'm using Knockout 2.3.0 with Durandal.JS and Breeze.JS

my computed looks like this. I'm using lodash map function to create a new object for each object in the array of rigs

        self.rigSelect = ko.dependentObservable(function () {
            var rigs = _(self.rigs()).map(function (rig) {
                return {
                    rig: rig,
                    name: '('+(rig.number())+ ') ' + (rig.name())
                };
            });
            return rigs.value();

        });
clekkas commented 10 years ago

Hi DrSammyD,

Can you provide a more complete example for me to work with, you can use either http://jsfiddle.net or http://plnkr.co to share your code.

One thing I did notice is that your computed is missing the second parameter, 'this'. Here is an explanation of what the second parameter is; from knockoutjs.com (http://knockoutjs.com/documentation/computedObservables.html)

"...This defines the value of this when evaluating the computed observable. Without passing it in, it would not have been possible to refer to this.firstName() or this.lastName()...."

So I would expect in your code something like this:

  self.rigSelect = ko.dependentObservable(function () {
            var rigs = _(self.rigs()).map(function (rig) {
                return {
                    rig: rig,
                    name: '('+(rig.number())+ ') ' + (rig.name())
                };
            });
            return rigs.value();

        }, this);

Hope this helps

mbest commented 10 years ago

@DrSammyD, what version of Deferred Updates? Can you provide a complete stack trace?

DrSammyD commented 10 years ago

Sorry guys, I'm having difficulty replicating this error. I went into the code and fixed it locally, then continued coding the rest of my view model, didn't check anything in because it was erring out.

I'm using version 3.0.0 I changed line 534 to

if ((!dependentObservable.equalityComparer || !dependentObservable.equalityComparer(_latestValue, newValue)) /*&& dependentObservable.notifySubscribers != undefined*/) 

which fixed the problem, but I'm not sure what else that might do to the code. My model looked like this

var vm = function () {
    var self = this;
    self.rigs = ko.observableArray();
    self.rigs(service.getRigs());
    self.rigSelect = ko.dependentObservable(function () {
        var rigs = _(self.rigs()).map(function (rig) {
            return {
                rig: rig,
                name: '('+(rig.number())+ ') ' + (rig.name())
            };
        });
        return rigs.value();
   });

};

When I took out my fix to get the stack trace, the error didn't come back. I'll let you know if/when it does.