Explorar el Código

almost there! scope nesting

Evan You hace 13 años
padre
commit
15ffaa4166
Se han modificado 5 ficheros con 33 adiciones y 12 borrados
  1. 1 1
      dev.html
  2. 1 3
      src/config.js
  3. 1 0
      src/directives.js
  4. 20 0
      src/main.js
  5. 10 8
      src/seed.js

+ 1 - 1
dev.html

@@ -27,7 +27,7 @@
                     sd-each="todo:todos"
                     sd-class="done:todo.done"
                     sd-on="click:changeMessage, click:todo.toggle"
-                    sd-text="todo.title"
+                    sd-text="msg"
                 ></li>
             </ul>
         </div>

+ 1 - 3
src/config.js

@@ -1,3 +1 @@
-module.exports = {
-    prefix: 'sd'
-}
+module.exports = {}

+ 1 - 0
src/directives.js

@@ -56,6 +56,7 @@ module.exports = {
             collection.forEach(function (item, i) {
                 self.childSeeds.push(self.buildItem(item, i, collection))
             })
+            console.log('collection creation done.')
         },
         mutate: function (mutation) {
             console.log(mutation)

+ 20 - 0
src/main.js

@@ -5,6 +5,25 @@ var config      = require('./config'),
     controllers = require('./controllers')
 
 Seed.config = config
+var prefix  = 'sd'
+Object.defineProperty(config, 'prefix', {
+    get: function () {
+        return prefix
+    },
+    set: function (value) {
+        prefix = value
+        updateSelector()
+    }
+})
+
+updateSelector()
+function updateSelector () {
+    config.selector = Object.keys(directives).map(function (key) {
+        return '[' + prefix + '-' + key + ']'
+    }).join()
+}
+
+// API
 
 Seed.extend = function (opts) {
     var Spore = function () {
@@ -49,6 +68,7 @@ Seed.bootstrap = function (seeds) {
 
 Seed.directive = function (name, fn) {
     directives[name] = fn
+    updateSelector()
 }
 
 Seed.filter = function (name, fn) {

+ 10 - 8
src/seed.js

@@ -39,18 +39,20 @@ function Seed (el, data, options) {
     }
 
     // process nodes for directives
+    // first, child with sd-each directive
+    
     this._compileNode(el, true)
 
-    // copy in methods from controller
-    if (controller) {
-        controller.call(null, this.scope, this)
-    }
-
     // initialize all variables by invoking setters
     for (key in dataCopy) {
         this.scope[key] = dataCopy[key]
     }
 
+    // copy in methods from controller
+    if (controller) {
+        controller.call(null, this.scope, this)
+    }
+
 }
 
 Seed.prototype._compileNode = function (node, root) {
@@ -110,15 +112,15 @@ Seed.prototype._bind = function (node, bindingInstance) {
     var key = bindingInstance.key,
         epr = this._options.eachPrefixRE,
         isEachKey = epr && epr.test(key),
-        seed = this
+        scopeOwner = this
     // TODO make scope chain work on nested controllers
     if (isEachKey) {
         key = key.replace(epr, '')
     } else if (epr) {
-        seed = this._options.parentSeed
+        scopeOwner = this._options.parentSeed
     }
 
-    var binding = seed._bindings[key] || seed._createBinding(key)
+    var binding = scopeOwner._bindings[key] || scopeOwner._createBinding(key)
 
     // add directive to this binding
     binding.instances.push(bindingInstance)