https://github.com/esbenp/knockout-change-subscriber
https://github.com/esbenp/knockout-change-subscriber
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/esbenp/knockout-change-subscriber
- Owner: esbenp
- License: apache-2.0
- Created: 2015-03-28T14:47:57.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2015-03-28T23:47:38.000Z (about 10 years ago)
- Last Synced: 2025-02-22T20:07:53.831Z (4 months ago)
- Language: JavaScript
- Size: 141 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# knockout-change-subscriber
A small subscribable extension to keep track of changes to an observable
# Why not subscribe(observable, null, 'arrayChange')?
The native array change subscribe function does not work well with computed arrays nor normal observables.
I.e. neither of these will work
```javascript
var observable = ko.observable();
var observableArray = ko.observableArray();
var computedObservableArray() = ko.computed(function(){
return observableArray();
}, this, "arrayChange");observable.subscribe(function(changes){
// never executes
}, null, "arrayChange");observableArray.subscribe(function(changes){
// works
}, null, "arrayChange");computedObservableArray.subscribe(function(changes){
// never fires
}, null, "arrayChange");
```# Dependencies
* jQuery >= 1.1.4 (used for $.extend)
* underscore.js ~1.8.2 (used for _.filter)
* bower-knockout-mapping ~2.5.0 (or just knockout-mapping)
* knockout.js >= 3.0.0# Installation
Can be installed via bower package```
bower install --save knockout-change-subscriber
```
... or by cloning the repository```
git clone [email protected]:esbenp/knockout-change-subscriber.git
```
... or by grabbing a zip of the latest release# Small example
See examples directory for more
```javascript
var observable = ko.observable("Observable 1");
var observableArray = ko.observableArray([
{id: ko.observable(2), name: ko.observable("Test")}
]);
var computedObservable = ko.computed(function(){
return observable();
}, this);
var computedObservableArray = ko.computed(function(){
return observableArray();
}, this);observable.changeSubscriber(function(newValue, oldValue){
console.log("observable", newValue, oldValue);
});observableArray.changeSubscriber(function(additions, deletions){
console.log("observableArray", additions, deletions);
});computedObservable.changeSubscriber(function(newValue, oldValue){
console.log("computedObservable change", newValue, oldValue);
});computedObservableArray.changeSubscriber(function(additions, deletions){
console.log("computedObservableArray change", additions, deletions);
});observable("Yo");
observableArray.push({id: ko.observable(3), name: ko.observable("Yo")});
observableArray.remove(function(entry){
return entry.id() === 2;
});
```# License
Copyright © 2015 Esben Petersen & ContributorsLicensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.