Knockout-2.2.0,订阅在更改前获取值和新值

时间:2012-12-28 23:00:57

标签: knockout-2.0

jsfiddle链接:http://jsfiddle.net/T8ee7/

当我调用Knockout的订阅方法时,我有没有办法获得前一个和新值?现在,我只能分别调用这些值。 如果旧值和新值不同,我想触发一些代码。

我想我可以做到以下几点,但它可能会变得混乱...... (http://jsfiddle.net/MV3fN/

var sv = sv || {};
sv.PagedRequest = function (pageNumber, pageSize) {
    this.pageNumber = ko.observable(pageNumber || 1);
    this.numberOfPages = ko.observable(1);
    this.pageSize = ko.observable(pageSize || sv.DefaultPageSize);
};


var _pagedRequest = new sv.PagedRequest();
var oldValue;
_pagedRequest.pageNumber.subscribe(function (previousValue) {
    console.log("old: " + previousValue);
    oldValue = previousValue;
}, _pagedRequest, "beforeChange");


_pagedRequest.pageNumber.subscribe(function (newValue) {

    console.log("new: " + newValue);
    if (oldValue != newValue) {
        console.log("value changed!");
    }

});

_pagedRequest.pageNumber(10);
_pagedRequest.pageNumber(20);
​

3 个答案:

答案 0 :(得分:22)

我更喜欢使用observable extender

http://jsfiddle.net/neonms92/xybGG/

<强>扩展器:

ko.extenders.withPrevious = function (target) {
    // Define new properties for previous value and whether it's changed
    target.previous = ko.observable();
    target.changed = ko.computed(function () { return target() !== target.previous(); });

    // Subscribe to observable to update previous, before change.
    target.subscribe(function (v) {
        target.previous(v);
    }, null, 'beforeChange');

    // Return modified observable
    return target;
}

使用示例:

// Define observable using 'withPrevious' extension
self.hours = ko.observable().extend({ withPrevious: 1 });

// Subscribe to observable like normal
self.hours.subscribe(function () {
    if (!self.hours.changed()) return; // Cancel if value hasn't changed
    print('Hours changed from ' + self.hours.previous() + ' to ' + self.hours());
});

答案 1 :(得分:10)

这似乎对我有用

ko.observable.fn.beforeAndAfterSubscribe = function (callback, target) {
 var _oldValue;
 this.subscribe(function (oldValue) {
     _oldValue = oldValue;
 }, null, 'beforeChange');
   this.subscribe(function (newValue) {
     callback.call(target, _oldValue, newValue);
 });
}; 

答案 2 :(得分:0)

http://jsfiddle.net/MV3fN/3/

var sv = sv || {};
sv.PagedRequest = function (pageNumber, pageSize) {
    var self = this;
    self.pageNumber = ko.observable(pageNumber || 1);
    self.numberOfPages = ko.observable(1);
    self.pageSize = ko.observable(pageSize || sv.DefaultPageSize);

    self.pageNumber.subscribe(function (previousValue) {
        console.log(previousValue);
        console.log(self.pageNumber.arguments[0]);
        if (previousValue != _pagedRequest.pageNumber.arguments[0]) {
            console.log('value changed');
        } 
        else {
            //This won't get executed because KO doesn't
            //call the function if the value doesn't change
            console.log('not changed');
        }
    }, _pagedRequest, "beforeChange");
};

var _pagedRequest = new sv.PagedRequest();

_pagedRequest.pageNumber(10);
_pagedRequest.pageNumber(20);
_pagedRequest.pageNumber(20);
_pagedRequest.pageNumber(5);

我不知道你是否真的应该使用arguments[0],但它似乎有效。

您还可以设置自己的方法,以更清洁的方式完成此任务:

http://jsfiddle.net/PXKgr/2/

...
    self.setPageNumber = function(page) {
        console.log(self.pageNumber());
        console.log(page);
        if (self.pageNumber() != page) {
            console.log('value changed');
        }
        else {
            console.log('not changed');
        }
        self.pageNumber(page);
    };
...
_pagedRequest.setPageNumber(10);
_pagedRequest.setPageNumber(20);
_pagedRequest.setPageNumber(20);
_pagedRequest.setPageNumber(5);