Comparing version 0.0.1 to 0.0.2
68
clctr.js
'use strict'; | ||
var collection = require('mout/collection'), | ||
obj = require('mout/object'), | ||
stampit = require('stampit'), | ||
EventEmitter = require('events').EventEmitter, | ||
var | ||
collection = require('./lib/collection.js'), | ||
model = require('./lib/model.js'); | ||
convertMethods = obj.mixIn({}, collection, { | ||
get: obj.get, | ||
hasOwn: obj.hasOwn | ||
}), | ||
// Export the collection stamp, and add model | ||
collection.model = model; | ||
nativeMethods = obj.map(convertMethods, function (method) { | ||
return function () { | ||
var args = [].slice.call(arguments); | ||
args.unshift(this); | ||
return method.apply(this.attrs, args); | ||
}; | ||
}), | ||
proto = obj.mixIn({}, nativeMethods, { | ||
set: function set(name, value) { | ||
var obj, prev, attrs = this.attrs; | ||
if (typeof name === 'object') { | ||
stampit.mixIn(attrs, obj); | ||
this.emit('set-collection', obj); | ||
} else { | ||
prev = attrs[name]; | ||
attrs[name] = value; | ||
this.emit('set', { | ||
name: name, | ||
value: value, | ||
previousValue: prev | ||
}); | ||
} | ||
return this; | ||
}, | ||
get: function get(name) { | ||
return this.attrs[name]; | ||
}, | ||
on: function on() { | ||
var events = this.events; | ||
events.on.apply(events, arguments); | ||
}, | ||
emit: function emit() { | ||
var events = this.events; | ||
events.emit.apply(events, arguments); | ||
} | ||
}), | ||
clctr = stampit().methods(proto).state({ | ||
attrs: {}, | ||
events: new EventEmitter() | ||
}); | ||
module.exports = clctr; | ||
module.exports = collection; |
{ | ||
"name": "clctr", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Event emitting collections with iterators (like Backbone.Collection).", | ||
@@ -8,3 +8,4 @@ "main": "clctr.js", | ||
"mout": "~0.6.0", | ||
"stampit": "~0.5.1" | ||
"stampit": "~0.5.1", | ||
"cuid": "~1.2.1" | ||
}, | ||
@@ -11,0 +12,0 @@ "scripts": { |
@@ -9,2 +9,15 @@ clctr | ||
# Events | ||
```js | ||
var co = require('./clctr.js'); | ||
var c = co(); | ||
c.on('set', function () { console.log([].slice.call(arguments)); }); | ||
c.set('bar', 'baz'); | ||
// [ { name: 'bar', value: 'baz', previousValue: undefined } ] | ||
``` | ||
## contains(list, value):Boolean | ||
@@ -170,12 +183,1 @@ | ||
# Events | ||
```js | ||
var co = require('./clctr.js'); | ||
var c = co(); | ||
c.on('set', function () { console.log([].slice.call(arguments)); }); | ||
c.set('bar', 'baz'); | ||
// [ { name: 'bar', value: 'baz', previousValue: undefined } ] | ||
``` |
'use strict'; | ||
var collection = require('../clctr.js'), | ||
var model = require('../clctr.js').model, | ||
test = require('tape'); | ||
test('.contains()', function (t) { | ||
var c = collection({name: 'test'}); | ||
var myModel = model({name: 'test'}); | ||
t.ok(c.contains('test'), | ||
t.ok(myModel.contains('test'), | ||
'should return true if a the object contains the value.'); | ||
@@ -14,7 +14,7 @@ | ||
test('.hasOwn()', function (t) { | ||
var c = collection({name: 'test'}); | ||
test('.get()', function (t) { | ||
var myModel = model({name: 'test'}); | ||
t.ok(c.hasOwn('name'), | ||
'should return true if the instance has the named property.'); | ||
t.ok(myModel.get('id'), | ||
'should return the value of the given named property.'); | ||
@@ -25,7 +25,7 @@ t.end(); | ||
test('.set()', function (t) { | ||
var c = collection({'name': 'old'}); | ||
var myModel = model({'name': 'old'}); | ||
c.set({name: 'value'}); | ||
myModel.set({name: 'value'}); | ||
t.ok(c.name, 'value', | ||
t.ok(myModel.name, 'value', | ||
'should set property.'); | ||
@@ -32,0 +32,0 @@ |
9436
9
146
182
3
+ Addedcuid@~1.2.1
+ Addedcuid@1.2.5(transitive)