Comparing version 0.0.0 to 0.0.2
{ | ||
"name": "ck-array", | ||
"version": "0.0.0", | ||
"version": "0.0.2", | ||
"description": "A simple observable array for JavaScript Frameworks", | ||
@@ -5,0 +5,0 @@ "main": "src/ckArray.js", |
# CK Array | ||
DO NOT USE THIS IMPLEMENTATION YET. IT IS FAR FROM DONE!! | ||
I like the Observer pattern. It is clear, it is smooth and it shines when combined with JavaScript and CoffeeScript. | ||
This is why I've created this little library which creates an Observable array, an array into which you can pass | ||
an observer and pass handlers. | ||
an observer and pass handlers. | ||
The following functionality is supported: | ||
initialize the CkArray: | ||
var CkArray = require('ck-array') | ||
var a = new CkArray() | ||
You can now pass in an observable by calling something like: | ||
var observable = { | ||
prePush: function(argument) {}, | ||
postPush: function(argument) {}, | ||
preSlice: function(arguments) {}, | ||
postSlice: function(result) {}, | ||
preSplice: function(arguments) {}, | ||
postSplice: function(result) {}, | ||
} | ||
The ```argument``` value is of course the argument(s) passed into the function. There is no way to cancel the action | ||
yet in the 'pre' functions. There is no way to rollback yet. The ```result``` value is the result from calling the | ||
function on the array. | ||
## Planned changes | ||
There are a few things which I'd like to add to this observable array implementation, I would like a way to cancel and | ||
rollback a method call. I would also like to add implementations for all of the other array methods. | ||
## What will stay the same? | ||
The 'thoughts' behind this implementation will not break in the coming releases. So most of the future extensions will | ||
be made to the observable object and the implementations of the other methods of the Array prototype. The current | ||
tests will not be broken. |
@@ -17,3 +17,4 @@ // Generated by CoffeeScript 1.9.1 | ||
expect(a).not.toBeNull(); | ||
return expect(a).not.toBeUndefined(); | ||
expect(a).not.toBeUndefined(); | ||
return expect(a instanceof Array).toMatch(/true/); | ||
}); | ||
@@ -29,3 +30,3 @@ it('should have a length property', function() { | ||
}); | ||
return it('should be able to observe changes and handle push messages', function() { | ||
it('should be able to observe changes and handle push messages', function() { | ||
var postItem, preItem; | ||
@@ -48,2 +49,49 @@ expect(a.observe).toBeDefined('CkArray does not have a pushObserve member'); | ||
}); | ||
it('should be able to observe changes from slice', function() { | ||
var CkArray, postSlice, preSlice; | ||
CkArray = require('./../src/CkArray'); | ||
a = new CkArray(); | ||
[1, 2, 3, 4, 5, 6, 7, 8, 9].forEach(function(n) { | ||
return a.push(n); | ||
}); | ||
preSlice = void 0; | ||
postSlice = void 0; | ||
a.observe({ | ||
preSlice: function() { | ||
return preSlice = {}; | ||
}, | ||
postSlice: function(items) { | ||
return postSlice = items; | ||
} | ||
}); | ||
a.slice(4, 6); | ||
expect(preSlice).not.toBeUndefined(); | ||
expect(postSlice).not.toBeUndefined(); | ||
expect(postSlice instanceof Array).toMatch(/true/); | ||
return expect(postSlice.length).toEqual(2, 'length is not correct'); | ||
}); | ||
return it('should be able to observe changes from splice', function() { | ||
var CkArray, postSplice, preSplice; | ||
CkArray = require('./../src/CkArray'); | ||
a = new CkArray(); | ||
[1, 2, 3, 4, 5, 6, 7, 8, 9].forEach(function(n) { | ||
return a.push(n); | ||
}); | ||
preSplice = void 0; | ||
postSplice = void 0; | ||
a.observe({ | ||
preSplice: function() { | ||
return preSplice = {}; | ||
}, | ||
postSplice: function(items) { | ||
return postSplice = items; | ||
} | ||
}); | ||
a.splice(4, 3); | ||
expect(preSplice).not.toBeUndefined(); | ||
expect(postSplice).not.toBeUndefined(); | ||
expect(postSplice instanceof Array).toMatch(/true/); | ||
expect(postSplice.length).toEqual(3, 'length is not correct'); | ||
return expect(a.length).toEqual(6, 'the original array is not modified, it\'s length should be 6'); | ||
}); | ||
}); | ||
@@ -50,0 +98,0 @@ |
@@ -10,3 +10,3 @@ // Generated by CoffeeScript 1.9.1 | ||
function CkArray() { | ||
function CkArray(items) { | ||
this.observers = []; | ||
@@ -34,6 +34,6 @@ this.push = function() { | ||
}); | ||
result = Array.prototype.splice.call(this, args); | ||
result = Array.prototype.splice.apply(this, args); | ||
this.observers.map(function(observer) { | ||
var ref; | ||
return (ref = observer.postSplice) != null ? ref.apply(null, args) : void 0; | ||
return (ref = observer.postSplice) != null ? ref.call(null, result) : void 0; | ||
}); | ||
@@ -47,8 +47,8 @@ return result; | ||
var ref; | ||
return (ref = observer.preSplice) != null ? ref.apply(null, args) : void 0; | ||
return (ref = observer.preSlice) != null ? ref.apply(null, args) : void 0; | ||
}); | ||
result = Array.prototype.slice.call(this, args); | ||
result = Array.prototype.slice.apply(this, args); | ||
this.observers.map(function(observer) { | ||
var ref; | ||
return (ref = observer.postSlice) != null ? ref.apply(null, args) : void 0; | ||
return (ref = observer.postSlice) != null ? ref.call(null, result) : void 0; | ||
}); | ||
@@ -55,0 +55,0 @@ return result; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
19946
165
37