Prechádzať zdrojové kódy

html and attr directives

Evan You 12 rokov pred
rodič
commit
f9077cfa6a
3 zmenil súbory, kde vykonal 23 pridanie a 7 odobranie
  1. 5 1
      examples/todos/app.js
  2. 3 3
      src/directives/each.js
  3. 15 3
      src/directives/index.js

+ 5 - 1
examples/todos/app.js

@@ -77,4 +77,8 @@ Seed.controller('Todos', function (scope) {
 
 })
 
-Seed.bootstrap()
+var s = Date.now()
+
+Seed.bootstrap()
+
+console.log(Date.now() - s)

+ 3 - 3
src/directives/each.js

@@ -19,7 +19,7 @@ var mutationHandlers = {
         m.args.forEach(function (data, i) {
             var seed = self.buildItem(data, i),
                 ref  = self.collection.length > m.args.length
-                     ? self.collection[m.args.length].$seed.el
+                     ? self.collection[m.args.length].$el
                      : self.marker
             self.container.insertBefore(seed.el, ref)
         })
@@ -45,7 +45,7 @@ var mutationHandlers = {
                 var seed = self.buildItem(data, index + i),
                     pos  = index - removed + added + 1,
                     ref  = self.collection[pos]
-                         ? self.collection[pos].$seed.el
+                         ? self.collection[pos].$el
                          : self.marker
                 self.container.insertBefore(seed.el, ref)
             })
@@ -59,7 +59,7 @@ var mutationHandlers = {
         var self = this
         self.collection.forEach(function (scope, i) {
             scope.$index = i
-            self.container.insertBefore(scope.$seed.el, self.marker)
+            self.container.insertBefore(scope.$el, self.marker)
         })
     }
 }

+ 15 - 3
src/directives/index.js

@@ -13,10 +13,20 @@ module.exports = {
     on    : require('./on'),
     each  : require('./each'),
 
+    attr: function (value) {
+        this.el.setAttribute(this.arg, value)  
+    },
+
     text: function (value) {
         this.el.textContent =
-            (value !== null && value !== undefined)
-            ? value.toString() : ''
+            (typeof value === 'string' || typeof value === 'number')
+            ? value : ''
+    },
+
+    html: function (value) {
+        this.el.innerHTML =
+            (typeof value === 'string' || typeof value === 'number')
+            ? value : ''
     },
 
     show: function (value) {
@@ -35,7 +45,9 @@ module.exports = {
         if (this.arg) {
             this.el.classList[value ? 'add' : 'remove'](this.arg)
         } else {
-            this.el.classList.remove(this.lastVal)
+            if (this.lastVal) {
+                this.el.classList.remove(this.lastVal)
+            }
             this.el.classList.add(value)
             this.lastVal = value
         }