0.2.1, remove $on/$off

This commit is contained in:
Evan You 2013-08-15 12:26:13 -04:00
parent a104afb472
commit e9f2223d3a
9 changed files with 30 additions and 104 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "seed", "name": "seed",
"version": "0.2.0", "version": "0.2.1",
"main": "dist/seed.js", "main": "dist/seed.js",
"ignore": [ "ignore": [
".*", ".*",

View File

@ -1,6 +1,6 @@
{ {
"name": "seed", "name": "seed",
"version": "0.2.0", "version": "0.2.1",
"main": "src/main.js", "main": "src/main.js",
"scripts": [ "scripts": [
"src/main.js", "src/main.js",

70
dist/seed.js vendored
View File

@ -469,7 +469,6 @@ var config = require('./config'),
Emitter = require('emitter'), Emitter = require('emitter'),
toString = Object.prototype.toString, toString = Object.prototype.toString,
aproto = Array.prototype, aproto = Array.prototype,
arrayMutators = ['push','pop','shift','unshift','splice','sort','reverse'],
templates = {} templates = {}
var arrayAugmentations = { var arrayAugmentations = {
@ -483,6 +482,20 @@ var arrayAugmentations = {
} }
} }
var arrayMutators = ['push','pop','shift','unshift','splice','sort','reverse'],
mutationInterceptors = {}
arrayMutators.forEach(function (method) {
mutationInterceptors[method] = function () {
var result = aproto[method].apply(this, arguments)
this.emit('mutate', {
method: method,
args: aproto.slice.call(arguments),
result: result
})
}
})
/* /*
* get accurate type of an object * get accurate type of an object
*/ */
@ -520,8 +533,6 @@ function dump (val) {
module.exports = { module.exports = {
// the global event bus
eventbus: new Emitter(),
typeOf: typeOf, typeOf: typeOf,
dump: dump, dump: dump,
@ -554,17 +565,7 @@ module.exports = {
var method, i = arrayMutators.length var method, i = arrayMutators.length
while (i--) { while (i--) {
method = arrayMutators[i] method = arrayMutators[i]
/* jshint loopfunc: true */ collection[method] = mutationInterceptors[method]
collection[method] = (function (method) {
return function () {
var result = aproto[method].apply(this, arguments)
this.emit('mutate', {
method: method,
args: aproto.slice.call(arguments),
result: result
})
}
})(method)
} }
for (method in arrayAugmentations) { for (method in arrayAugmentations) {
collection[method] = arrayAugmentations[method] collection[method] = arrayAugmentations[method]
@ -598,8 +599,7 @@ var config = require('./config'),
Binding = require('./binding'), Binding = require('./binding'),
DirectiveParser = require('./directive-parser'), DirectiveParser = require('./directive-parser'),
TextParser = require('./text-parser'), TextParser = require('./text-parser'),
DepsParser = require('./deps-parser'), DepsParser = require('./deps-parser')
eventbus = require('./utils').eventbus
var slice = Array.prototype.slice, var slice = Array.prototype.slice,
ctrlAttr = config.prefix + '-controller', ctrlAttr = config.prefix + '-controller',
@ -625,7 +625,6 @@ function Compiler (vm, options) {
this.bindings = {} this.bindings = {}
this.directives = [] this.directives = []
this.watchers = {} this.watchers = {}
this.listeners = []
// list of computed properties that need to parse dependencies for // list of computed properties that need to parse dependencies for
this.computed = [] this.computed = []
// list of bindings that has dynamic context dependencies // list of bindings that has dynamic context dependencies
@ -853,7 +852,7 @@ CompilerProto.bindContexts = function (bindings) {
*/ */
CompilerProto.destroy = function () { CompilerProto.destroy = function () {
utils.log('compiler destroyed: ', this.vm.$el) utils.log('compiler destroyed: ', this.vm.$el)
var i, key, dir, listener, inss var i, key, dir, inss
// remove all directives that are instances of external bindings // remove all directives that are instances of external bindings
i = this.directives.length i = this.directives.length
while (i--) { while (i--) {
@ -864,12 +863,6 @@ CompilerProto.destroy = function () {
} }
dir.unbind() dir.unbind()
} }
// remove all listeners on eventbus
i = this.listeners.length
while (i--) {
listener = this.listeners[i]
eventbus.off(listener.event, listener.handler)
}
// unbind all bindings // unbind all bindings
for (key in this.bindings) { for (key in this.bindings) {
this.bindings[key].unbind() this.bindings[key].unbind()
@ -927,33 +920,6 @@ function ViewModel (options) {
var VMProto = ViewModel.prototype var VMProto = ViewModel.prototype
/*
* register a listener that will be broadcasted from the global event bus
*/
VMProto.$on = function (event, handler) {
utils.eventbus.on(event, handler)
this.$compiler.listeners.push({
event: event,
handler: handler
})
}
/*
* remove the registered listener
*/
VMProto.$off = function (event, handler) {
utils.eventbus.off(event, handler)
var listeners = this.$compiler.listeners,
i = listeners.length, listener
while (i--) {
listener = listeners[i]
if (listener.event === event && listener.handler === handler) {
listeners.splice(i, 1)
break
}
}
}
/* /*
* watch a key on the viewmodel for changes * watch a key on the viewmodel for changes
* fire callback with new value * fire callback with new value
@ -1993,5 +1959,5 @@ require.alias("component-indexof/index.js", "component-emitter/deps/indexof/inde
require.alias("seed/src/main.js", "seed/index.js"); require.alias("seed/src/main.js", "seed/index.js");
window.Seed = window.Seed || require('seed') window.Seed = window.Seed || require('seed')
Seed.version = '0.2.0' Seed.version = '0.2.1'
})(); })();

2
dist/seed.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -4,19 +4,12 @@ var filters = {
completed: function (todo) { return todo.completed } completed: function (todo) { return todo.completed }
} }
window.addEventListener('hashchange', function () {
Seed.broadcast('filterchange')
})
var Todos = Seed.ViewModel.extend({ var Todos = Seed.ViewModel.extend({
initialize: function () { initialize: function () {
// listen for hashtag change
this.updateFilter()
this.$on('filterchange', this.updateFilter.bind(this))
// instance properties
this.todos = todoStorage.fetch() this.todos = todoStorage.fetch()
this.remaining = this.todos.filter(filters.active).length this.remaining = this.todos.filter(filters.active).length
this.updateFilter()
}, },
properties: { properties: {
@ -107,3 +100,7 @@ var Todos = Seed.ViewModel.extend({
}) })
var app = new Todos({ el: '#todoapp' }) var app = new Todos({ el: '#todoapp' })
window.addEventListener('hashchange', function () {
app.updateFilter()
})

View File

@ -1,6 +1,6 @@
{ {
"name": "seed", "name": "seed",
"version": "0.2.0", "version": "0.2.1",
"devDependencies": { "devDependencies": {
"grunt": "~0.4.1", "grunt": "~0.4.1",
"grunt-contrib-watch": "~0.4.4", "grunt-contrib-watch": "~0.4.4",

View File

@ -3,8 +3,7 @@ var config = require('./config'),
Binding = require('./binding'), Binding = require('./binding'),
DirectiveParser = require('./directive-parser'), DirectiveParser = require('./directive-parser'),
TextParser = require('./text-parser'), TextParser = require('./text-parser'),
DepsParser = require('./deps-parser'), DepsParser = require('./deps-parser')
eventbus = require('./utils').eventbus
var slice = Array.prototype.slice, var slice = Array.prototype.slice,
ctrlAttr = config.prefix + '-controller', ctrlAttr = config.prefix + '-controller',
@ -30,7 +29,6 @@ function Compiler (vm, options) {
this.bindings = {} this.bindings = {}
this.directives = [] this.directives = []
this.watchers = {} this.watchers = {}
this.listeners = []
// list of computed properties that need to parse dependencies for // list of computed properties that need to parse dependencies for
this.computed = [] this.computed = []
// list of bindings that has dynamic context dependencies // list of bindings that has dynamic context dependencies
@ -258,7 +256,7 @@ CompilerProto.bindContexts = function (bindings) {
*/ */
CompilerProto.destroy = function () { CompilerProto.destroy = function () {
utils.log('compiler destroyed: ', this.vm.$el) utils.log('compiler destroyed: ', this.vm.$el)
var i, key, dir, listener, inss var i, key, dir, inss
// remove all directives that are instances of external bindings // remove all directives that are instances of external bindings
i = this.directives.length i = this.directives.length
while (i--) { while (i--) {
@ -269,12 +267,6 @@ CompilerProto.destroy = function () {
} }
dir.unbind() dir.unbind()
} }
// remove all listeners on eventbus
i = this.listeners.length
while (i--) {
listener = this.listeners[i]
eventbus.off(listener.event, listener.handler)
}
// unbind all bindings // unbind all bindings
for (key in this.bindings) { for (key in this.bindings) {
this.bindings[key].unbind() this.bindings[key].unbind()

View File

@ -66,8 +66,6 @@ function dump (val) {
module.exports = { module.exports = {
// the global event bus
eventbus: new Emitter(),
typeOf: typeOf, typeOf: typeOf,
dump: dump, dump: dump,

View File

@ -25,33 +25,6 @@ function ViewModel (options) {
var VMProto = ViewModel.prototype var VMProto = ViewModel.prototype
/*
* register a listener that will be broadcasted from the global event bus
*/
VMProto.$on = function (event, handler) {
utils.eventbus.on(event, handler)
this.$compiler.listeners.push({
event: event,
handler: handler
})
}
/*
* remove the registered listener
*/
VMProto.$off = function (event, handler) {
utils.eventbus.off(event, handler)
var listeners = this.$compiler.listeners,
i = listeners.length, listener
while (i--) {
listener = listeners[i]
if (listener.event === event && listener.handler === handler) {
listeners.splice(i, 1)
break
}
}
}
/* /*
* watch a key on the viewmodel for changes * watch a key on the viewmodel for changes
* fire callback with new value * fire callback with new value