Browse Source

solved the setter init invoke

Evan You 13 năm trước cách đây
mục cha
commit
14d0cefe6b
3 tập tin đã thay đổi với 28 bổ sung48 xóa
  1. 5 3
      dev/todos.html
  2. 4 3
      src/main.js
  3. 19 42
      src/seed.js

+ 5 - 3
dev/todos.html

@@ -29,7 +29,7 @@
         </style>
     </head>
     <body>
-        <div id="app" sd-controller="Todos" sd-class="filter">
+        <div id="app" sd-controller="Todos" sd-data="test" sd-class="filter">
             <div>
                 <input placeholder="What needs to be done?" sd-on="change:addTodo">
             </div>
@@ -62,12 +62,14 @@
                 { text: 'parse textnodes', done: false }
             ]
 
+            Seed.data('test', { todos: todos })
+
             Seed.controller('Todos', function (scope, seed) {
 
                 // regular properties
-                scope.todos = todos
+                // scope.todos = todos
                 scope.filter = 'all'
-                scope.remaining = scope.todos.reduce(function (count, todo) {
+                scope.remaining = todos.reduce(function (count, todo) {
                     return count + (todo.done ? 0 : 1)
                 }, 0)
 

+ 4 - 3
src/main.js

@@ -9,8 +9,6 @@ var controllers = config.controllers = {},
 
 // API
 
-api.config = config
-
 api.extend = function (opts) {
     var Spore = function () {
         Seed.apply(this, arguments)
@@ -45,7 +43,10 @@ api.controller = function (id, extensions) {
     controllers[id] = extensions
 }
 
-api.bootstrap = function () {
+api.bootstrap = function (opts) {
+    if (opts) {
+        config.prefix = opts.prefix || config.prefix
+    }
     var app = {},
         n = 0,
         el, seed

+ 19 - 42
src/seed.js

@@ -2,21 +2,14 @@ var Emitter         = require('emitter'),
     config          = require('./config'),
     DirectiveParser = require('./directive-parser')
 
-var slice = Array.prototype.slice
-
-var ancestorKeyRE = /\^/g,
-    rootKeyRE = /^\$/
-
-// lazy init
-var ctrlAttr,
-    eachAttr
+var slice           = Array.prototype.slice,
+    ancestorKeyRE   = /\^/g,
+    rootKeyRE       = /^\$/,
+    ctrlAttr        = config.prefix + '-controller',
+    eachAttr        = config.prefix + '-each'
 
 function Seed (el, options) {
 
-    // refresh
-    ctrlAttr = config.prefix + '-controller'
-    eachAttr = config.prefix + '-each'
-
     if (typeof el === 'string') {
         el = document.querySelector(el)
     }
@@ -24,31 +17,22 @@ function Seed (el, options) {
     el.seed        = this
     this.el        = el
     this._bindings = {}
-    this.components = {}
 
+    // copy options
     if (options) {
         for (var op in options) {
             this[op] = options[op]
         }
     }
 
-    // initiate the scope
+    // initialize the scope object
     var dataPrefix = config.prefix + '-data'
     this.scope =
-        (options && options.data)
-        || config.datum[el.getAttribute(dataPrefix)]
-        || {}
+            (options && options.data)
+            || config.datum[el.getAttribute(dataPrefix)]
+            || {}
     el.removeAttribute(dataPrefix)
 
-    // keep a temporary copy for all the real data
-    // so we can overwrite the passed in data object
-    // with getter/setters.
-    var key
-    this._dataCopy = {}
-    for (key in this.scope) {
-        this._dataCopy[key] = this.scope[key]
-    }
-
     // if has controller
     var ctrlID = el.getAttribute(ctrlAttr),
         controller = null
@@ -58,17 +42,9 @@ function Seed (el, options) {
         el.removeAttribute(ctrlAttr)
     }
 
-    // process nodes for directives
-    // first, child with sd-each directive
-    
+    // revursively process nodes for directives
     this._compileNode(el, true)
 
-    // initialize all variables by invoking setters
-    for (key in this._dataCopy) {
-        this.scope[key] = this._dataCopy[key]
-    }
-    delete this._dataCopy
-
     // copy in methods from controller
     if (controller) {
         controller.call(this, this.scope, this)
@@ -94,11 +70,6 @@ Seed.prototype._compileNode = function (node, root) {
             var binding = DirectiveParser.parse(eachAttr, eachExp)
             if (binding) {
                 self._bind(node, binding)
-                // need to set each block now so it can inherit
-                // parent scope. i.e. the childSeeds must have been
-                // initiated when parent scope setters are invoked
-                self.scope[binding.key] = self._dataCopy[binding.key]
-                ;delete self._dataCopy[binding.key]
             }
 
         } else if (ctrlExp && !root) { // nested controllers
@@ -111,7 +82,7 @@ Seed.prototype._compileNode = function (node, root) {
                 self['$' + id] = seed
             }
 
-        } else if (node.attributes && node.attributes.length) { // normal node (non-controller)
+        } else if (node.attributes && node.attributes.length) { // normal node
 
             slice.call(node.attributes).forEach(function (attr) {
                 var valid = false
@@ -126,6 +97,7 @@ Seed.prototype._compileNode = function (node, root) {
             })
         }
 
+        // recursively parse child nodes
         if (!eachExp && !ctrlExp) {
             if (node.childNodes.length) {
                 slice.call(node.childNodes).forEach(function (child) {
@@ -186,12 +158,17 @@ Seed.prototype._bind = function (node, directive) {
         directive.bind(binding.value)
     }
 
+    // set initial value
+    if (binding.value) {
+        directive.update(binding.value)
+    }
+
 }
 
 Seed.prototype._createBinding = function (key) {
 
     var binding = {
-        value: null,
+        value: this.scope[key],
         instances: []
     }