Przeglądaj źródła

0.2.1, remove $on/$off

Evan You 12 lat temu
rodzic
commit
e9f2223d3a
9 zmienionych plików z 29 dodań i 103 usunięć
  1. 1 1
      bower.json
  2. 1 1
      component.json
  3. 18 52
      dist/seed.js
  4. 0 0
      dist/seed.min.js
  5. 6 9
      examples/todomvc/js/app.js
  6. 1 1
      package.json
  7. 2 10
      src/compiler.js
  8. 0 2
      src/utils.js
  9. 0 27
      src/viewmodel.js

+ 1 - 1
bower.json

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

+ 1 - 1
component.json

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

+ 18 - 52
dist/seed.js

@@ -469,7 +469,6 @@ var config        = require('./config'),
     Emitter       = require('emitter'),
     toString      = Object.prototype.toString,
     aproto        = Array.prototype,
-    arrayMutators = ['push','pop','shift','unshift','splice','sort','reverse'],
     templates     = {}
 
 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
  */
@@ -520,8 +533,6 @@ function dump (val) {
 
 module.exports = {
 
-    // the global event bus
-    eventbus: new Emitter(),
     typeOf: typeOf,
     dump: dump,
 
@@ -554,17 +565,7 @@ module.exports = {
         var method, i = arrayMutators.length
         while (i--) {
             method = arrayMutators[i]
-            /* jshint loopfunc: true */
-            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)
+            collection[method] = mutationInterceptors[method]
         }
         for (method in arrayAugmentations) {
             collection[method] = arrayAugmentations[method]
@@ -598,8 +599,7 @@ var config          = require('./config'),
     Binding         = require('./binding'),
     DirectiveParser = require('./directive-parser'),
     TextParser      = require('./text-parser'),
-    DepsParser      = require('./deps-parser'),
-    eventbus        = require('./utils').eventbus
+    DepsParser      = require('./deps-parser')
 
 var slice           = Array.prototype.slice,
     ctrlAttr        = config.prefix + '-controller',
@@ -625,7 +625,6 @@ function Compiler (vm, options) {
     this.bindings        = {}
     this.directives      = []
     this.watchers        = {}
-    this.listeners       = []
     // list of computed properties that need to parse dependencies for
     this.computed        = []
     // list of bindings that has dynamic context dependencies
@@ -853,7 +852,7 @@ CompilerProto.bindContexts = function (bindings) {
  */
 CompilerProto.destroy = function () {
     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
     i = this.directives.length
     while (i--) {
@@ -864,12 +863,6 @@ CompilerProto.destroy = function () {
         }
         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
     for (key in this.bindings) {
         this.bindings[key].unbind()
@@ -927,33 +920,6 @@ function ViewModel (options) {
 
 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
  *  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");
 
 window.Seed = window.Seed || require('seed')
-Seed.version = '0.2.0'
+Seed.version = '0.2.1'
 })();

Plik diff jest za duży
+ 0 - 0
dist/seed.min.js


+ 6 - 9
examples/todomvc/js/app.js

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

+ 1 - 1
package.json

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

+ 2 - 10
src/compiler.js

@@ -3,8 +3,7 @@ var config          = require('./config'),
     Binding         = require('./binding'),
     DirectiveParser = require('./directive-parser'),
     TextParser      = require('./text-parser'),
-    DepsParser      = require('./deps-parser'),
-    eventbus        = require('./utils').eventbus
+    DepsParser      = require('./deps-parser')
 
 var slice           = Array.prototype.slice,
     ctrlAttr        = config.prefix + '-controller',
@@ -30,7 +29,6 @@ function Compiler (vm, options) {
     this.bindings        = {}
     this.directives      = []
     this.watchers        = {}
-    this.listeners       = []
     // list of computed properties that need to parse dependencies for
     this.computed        = []
     // list of bindings that has dynamic context dependencies
@@ -258,7 +256,7 @@ CompilerProto.bindContexts = function (bindings) {
  */
 CompilerProto.destroy = function () {
     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
     i = this.directives.length
     while (i--) {
@@ -269,12 +267,6 @@ CompilerProto.destroy = function () {
         }
         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
     for (key in this.bindings) {
         this.bindings[key].unbind()

+ 0 - 2
src/utils.js

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

+ 0 - 27
src/viewmodel.js

@@ -25,33 +25,6 @@ function ViewModel (options) {
 
 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
  *  fire callback with new value

Niektóre pliki nie zostały wyświetlone z powodu dużej ilości zmienionych plików