2012-12-28 69 views
18

的jsfiddle鏈接獲得價值:http://jsfiddle.net/T8ee7/淘汰賽-2.2.0,訂閱前的變化和新的價值

當我打電話淘汰賽的訂閱方法有沒有方法可以讓我同時獲得了以前和現在的價值?現在,我只能分別調用這些值。 如果舊值和新值不同,我想觸發一些代碼。

我想我可以做到以下幾點,但它會導致混亂... (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); 
​ 
+0

在http://stackoverflow.com/questions/12822954/get-previous-value回答-of-AN-觀察到,在訂閱-的,同樣可觀察 – Nishanthan

回答

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); 
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); 
}); 
}; 
21

我更喜歡使用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()); 
});