Просмотр исходного кода

rename `prop` option to `scope`

Evan You 12 лет назад
Родитель
Сommit
99b25b2998

+ 1 - 1
TODO.md

@@ -1,4 +1,4 @@
-- put all API methods on Seed
+- Array.replace should not do anything if array is empty
 - add a method to observe additional data properties
 - add a method to observe additional data properties
 - add `lazy` option
 - add `lazy` option
 - add directive for all form elements, and make it consistent as `sd-model`
 - add directive for all form elements, and make it consistent as `sd-model`

+ 1 - 1
examples/encapsulation.html

@@ -22,7 +22,7 @@
             })
             })
             new T({
             new T({
                 el: '#test',
                 el: '#test',
-                data: {
+                scope: {
                     hi: 'ho43h132oh5o12345'
                     hi: 'ho43h132oh5o12345'
                 }
                 }
             })
             })

+ 1 - 1
examples/expression.html

@@ -14,7 +14,7 @@
             Seed.config({ debug: true })
             Seed.config({ debug: true })
             var demo = new Seed({
             var demo = new Seed({
                 el: '#demo',
                 el: '#demo',
-                data: {
+                scope: {
                     one: 'Hello',
                     one: 'Hello',
                     two: 'World'
                     two: 'World'
                 }
                 }

+ 1 - 1
examples/nested-props.html

@@ -53,7 +53,7 @@
             var app = new Demo({ el: '#a' }),
             var app = new Demo({ el: '#a' }),
                 app2 = new Seed({
                 app2 = new Seed({
                     el: '#b',
                     el: '#b',
-                    data: {
+                    scope: {
                         a: data
                         a: data
                     }
                     }
                 })
                 })

+ 1 - 1
examples/repeated-items.html

@@ -37,7 +37,7 @@
 
 
             var demo = new Seed({
             var demo = new Seed({
                 el: '#app',
                 el: '#app',
-                data: {
+                scope: {
                     items: items,
                     items: items,
                     push: function () {
                     push: function () {
                         this.items.push({ title: randomChar() })
                         this.items.push({ title: randomChar() })

+ 4 - 4
examples/share-data.html

@@ -20,25 +20,25 @@
             }
             }
             new Seed({
             new Seed({
                 el: '#a',
                 el: '#a',
-                data: {
+                scope: {
                     shared: shared
                     shared: shared
                 }
                 }
             })
             })
             new Seed({
             new Seed({
                 el: '#b',
                 el: '#b',
-                data: {
+                scope: {
                     shared: shared
                     shared: shared
                 }
                 }
             })
             })
             new Seed({
             new Seed({
                 el: '#c',
                 el: '#c',
-                data: {
+                scope: {
                     shared: shared
                     shared: shared
                 }
                 }
             })
             })
             new Seed({
             new Seed({
                 el: '#d',
                 el: '#d',
-                data: {
+                scope: {
                     shared: shared,
                     shared: shared,
                     source: {
                     source: {
                         get: function () {
                         get: function () {

+ 1 - 1
examples/todomvc/js/app.js

@@ -13,7 +13,7 @@ var app = new Seed({
         this.updateFilter()
         this.updateFilter()
     },
     },
 
 
-    data: {
+    scope: {
 
 
         todos: todoStorage.fetch(),
         todos: todoStorage.fetch(),
 
 

+ 3 - 3
src/compiler.js

@@ -31,9 +31,9 @@ function Compiler (vm, options) {
     compiler.setupElement(options)
     compiler.setupElement(options)
     utils.log('\nnew VM instance: ', compiler.el, '\n')
     utils.log('\nnew VM instance: ', compiler.el, '\n')
 
 
-    // copy data to vm
-    var data = options.data
-    if (data) utils.extend(vm, data)
+    // copy scope properties to vm
+    var scope = options.scope
+    if (scope) utils.extend(vm, scope)
 
 
     compiler.vm  = vm
     compiler.vm  = vm
     vm.$compiler = compiler
     vm.$compiler = compiler

+ 3 - 3
src/directives/repeat.js

@@ -141,11 +141,11 @@ module.exports = {
             ctn  = this.container,
             ctn  = this.container,
             vmID = node.getAttribute(config.prefix + '-viewmodel'),
             vmID = node.getAttribute(config.prefix + '-viewmodel'),
             ChildVM = this.compiler.getOption('vms', vmID) || ViewModel,
             ChildVM = this.compiler.getOption('vms', vmID) || ViewModel,
-            wrappedData = {}
-        wrappedData[this.arg] = data || {}
+            scope = {}
+        scope[this.arg] = data || {}
         var item = new ChildVM({
         var item = new ChildVM({
             el: node,
             el: node,
-            data: wrappedData,
+            scope: scope,
             compilerOptions: {
             compilerOptions: {
                 repeat: true,
                 repeat: true,
                 repeatIndex: index,
                 repeatIndex: index,

+ 1 - 1
src/main.js

@@ -95,7 +95,7 @@ function extend (options) {
 /*
 /*
  *  Inherit options
  *  Inherit options
  *
  *
- *  For options such as `data`, `vms`, `directives`, 'partials',
+ *  For options such as `scope`, `vms`, `directives`, 'partials',
  *  they should be further extended. However extending should only
  *  they should be further extended. However extending should only
  *  be done at top level.
  *  be done at top level.
  *  
  *  

+ 23 - 23
test/unit/specs/api.js

@@ -10,7 +10,7 @@ describe('UNIT: API', function () {
             mock(testId, '<span test-text="test"></span>')
             mock(testId, '<span test-text="test"></span>')
             new Seed({
             new Seed({
                 el: '#' + testId,
                 el: '#' + testId,
-                data: { test: testId }
+                scope: { test: testId }
             })
             })
             assert.strictEqual($('#' + testId + ' span'), testId)
             assert.strictEqual($('#' + testId + ' span'), testId)
         })
         })
@@ -26,7 +26,7 @@ describe('UNIT: API', function () {
             mock(testId, '<% test %>')
             mock(testId, '<% test %>')
             new Seed({
             new Seed({
                 el: '#' + testId,
                 el: '#' + testId,
-                data: { test: testId }
+                scope: { test: testId }
             })
             })
             assert.strictEqual($('#' + testId), testId)
             assert.strictEqual($('#' + testId), testId)
         })
         })
@@ -56,7 +56,7 @@ describe('UNIT: API', function () {
             mock(testId, '{{ test | reverse }}')
             mock(testId, '{{ test | reverse }}')
             new Seed({
             new Seed({
                 el: '#' + testId,
                 el: '#' + testId,
-                data: { test: msg }
+                scope: { test: msg }
             })
             })
             assert.strictEqual($('#' + testId), '54321')
             assert.strictEqual($('#' + testId), '54321')
         })
         })
@@ -81,7 +81,7 @@ describe('UNIT: API', function () {
             mock(testId, '<span sd-test="test"></span>')
             mock(testId, '<span sd-test="test"></span>')
             new Seed({
             new Seed({
                 el: '#' + testId,
                 el: '#' + testId,
-                data: { test: msg }
+                scope: { test: msg }
             })
             })
             var el = document.querySelector('#' + testId + ' span')
             var el = document.querySelector('#' + testId + ' span')
             assert.strictEqual(el.getAttribute(testId), msg + '123')
             assert.strictEqual(el.getAttribute(testId), msg + '123')
@@ -105,7 +105,7 @@ describe('UNIT: API', function () {
             mock(testId, '<span sd-test2="test"></span>')
             mock(testId, '<span sd-test2="test"></span>')
             var vm = new Seed({
             var vm = new Seed({
                     el: '#' + testId,
                     el: '#' + testId,
-                    data: { test: msg }
+                    scope: { test: msg }
                 }),
                 }),
                 el = document.querySelector('#' + testId + ' span')
                 el = document.querySelector('#' + testId + ' span')
             assert.strictEqual(el.getAttribute(testId + 'bind'), msg + 'bind', 'should have called bind()')
             assert.strictEqual(el.getAttribute(testId + 'bind'), msg + 'bind', 'should have called bind()')
@@ -126,7 +126,7 @@ describe('UNIT: API', function () {
         var testId = 'api-vm-test',
         var testId = 'api-vm-test',
             Test = Seed.extend({
             Test = Seed.extend({
                 className: 'hihi',
                 className: 'hihi',
-                data: { hi: 'ok' }
+                scope: { hi: 'ok' }
             }),
             }),
             utils = require('seed/src/utils')
             utils = require('seed/src/utils')
 
 
@@ -173,7 +173,7 @@ describe('UNIT: API', function () {
             mock(testId, '<div class="directive" sd-partial="' + testId + '">hello</div>')
             mock(testId, '<div class="directive" sd-partial="' + testId + '">hello</div>')
             var t = new Seed({
             var t = new Seed({
                 el: '#' + testId,
                 el: '#' + testId,
-                data: { hi: 'hohoho' }
+                scope: { hi: 'hohoho' }
             })
             })
             assert.strictEqual(t.$el.querySelector('.directive .partial-test a').textContent, 'hohoho')
             assert.strictEqual(t.$el.querySelector('.directive .partial-test a').textContent, 'hohoho')
             assert.strictEqual(t.$el.querySelector('.directive span').innerHTML, 'hahaha')
             assert.strictEqual(t.$el.querySelector('.directive span').innerHTML, 'hahaha')
@@ -185,7 +185,7 @@ describe('UNIT: API', function () {
             mock(testId, '<div class="inline">{{>' + testId + '}}</div>')
             mock(testId, '<div class="inline">{{>' + testId + '}}</div>')
             var t = new Seed({
             var t = new Seed({
                 el: '#' + testId,
                 el: '#' + testId,
-                data: { hi: 'hohoho' }
+                scope: { hi: 'hohoho' }
             })
             })
             assert.strictEqual(t.$el.querySelector('.inline .partial-test a').textContent, 'hohoho')
             assert.strictEqual(t.$el.querySelector('.inline .partial-test a').textContent, 'hohoho')
             assert.strictEqual(t.$el.querySelector('.inline span').innerHTML, 'hahaha')
             assert.strictEqual(t.$el.querySelector('.inline span').innerHTML, 'hahaha')
@@ -222,12 +222,12 @@ describe('UNIT: API', function () {
 
 
         it('should allow further extensions', function () {
         it('should allow further extensions', function () {
             var Parent = Seed.extend({
             var Parent = Seed.extend({
-                data: {
+                scope: {
                     test: 'hi'
                     test: 'hi'
                 }
                 }
             })
             })
             var Child = Parent.extend({
             var Child = Parent.extend({
-                data: {
+                scope: {
                     test2: 'ho',
                     test2: 'ho',
                     test3: {
                     test3: {
                         hi: 1
                         hi: 1
@@ -236,7 +236,7 @@ describe('UNIT: API', function () {
             })
             })
             assert.strictEqual(Child.super, Parent)
             assert.strictEqual(Child.super, Parent)
             var child = new Child({
             var child = new Child({
-                data: {
+                scope: {
                     test3: {
                     test3: {
                         ho: 2
                         ho: 2
                     }
                     }
@@ -280,12 +280,12 @@ describe('UNIT: API', function () {
 
 
             })
             })
 
 
-            describe('data', function () {
+            describe('scope', function () {
                 
                 
                 it('should be copied to each instance', function () {
                 it('should be copied to each instance', function () {
                     var testData = { a: 1 },
                     var testData = { a: 1 },
                         Test = Seed.extend({
                         Test = Seed.extend({
-                            data: {
+                            scope: {
                                 test: testData
                                 test: testData
                             }
                             }
                         })
                         })
@@ -317,7 +317,7 @@ describe('UNIT: API', function () {
                             'test': 'hi',
                             'test': 'hi',
                             'sd-text': 'hoho'
                             'sd-text': 'hoho'
                         },
                         },
-                        data: {
+                        scope: {
                             hoho: 'what'
                             hoho: 'what'
                         }
                         }
                     })
                     })
@@ -339,7 +339,7 @@ describe('UNIT: API', function () {
                                 'test': 'hi',
                                 'test': 'hi',
                                 'sd-text': 'hoho'
                                 'sd-text': 'hoho'
                             },
                             },
-                            data: {
+                            scope: {
                                 hoho: 'what'
                                 hoho: 'what'
                             }
                             }
                         }),
                         }),
@@ -364,7 +364,7 @@ describe('UNIT: API', function () {
                     var Test = Seed.extend({
                     var Test = Seed.extend({
                             tagName: 'p',
                             tagName: 'p',
                             template: raw,
                             template: raw,
-                            data: {
+                            scope: {
                                 hello: 'Ahaha'
                                 hello: 'Ahaha'
                             }
                             }
                         }),
                         }),
@@ -385,7 +385,7 @@ describe('UNIT: API', function () {
                     document.getElementById('test').appendChild(tpl)
                     document.getElementById('test').appendChild(tpl)
                     var Test = Seed.extend({
                     var Test = Seed.extend({
                         template: '#' + testId,
                         template: '#' + testId,
-                        data: { hello: testId }
+                        scope: { hello: testId }
                     })
                     })
                     var t = new Test()
                     var t = new Test()
                     assert.strictEqual(t.$el.querySelector('span').textContent, testId)
                     assert.strictEqual(t.$el.querySelector('span').textContent, testId)
@@ -397,7 +397,7 @@ describe('UNIT: API', function () {
                     })
                     })
                     var t = new Test({
                     var t = new Test({
                         template: raw,
                         template: raw,
-                        data: {
+                        scope: {
                             hello: 'overwritten!'
                             hello: 'overwritten!'
                         }
                         }
                     })
                     })
@@ -420,7 +420,7 @@ describe('UNIT: API', function () {
                         attributes: {
                         attributes: {
                             'sd-test': 'ok'
                             'sd-test': 'ok'
                         },
                         },
-                        data: {
+                        scope: {
                             ok: true
                             ok: true
                         }
                         }
                     })
                     })
@@ -443,7 +443,7 @@ describe('UNIT: API', function () {
                     })
                     })
                     var t = new Test({
                     var t = new Test({
                         template: '{{hi | test}}',
                         template: '{{hi | test}}',
-                        data: {
+                        scope: {
                             hi: 'hohoho'
                             hi: 'hohoho'
                         }
                         }
                     })
                     })
@@ -456,13 +456,13 @@ describe('UNIT: API', function () {
 
 
                 it('should allow the VM to use private child VMs', function () {
                 it('should allow the VM to use private child VMs', function () {
                     var Child = Seed.extend({
                     var Child = Seed.extend({
-                        data: {
+                        scope: {
                             name: 'child'
                             name: 'child'
                         }
                         }
                     })
                     })
                     var Parent = Seed.extend({
                     var Parent = Seed.extend({
                         template: '<p>{{name}}</p><div sd-viewmodel="child">{{name}}</div>',
                         template: '<p>{{name}}</p><div sd-viewmodel="child">{{name}}</div>',
-                        data: {
+                        scope: {
                             name: 'dad'
                             name: 'dad'
                         },
                         },
                         vms: {
                         vms: {
@@ -486,7 +486,7 @@ describe('UNIT: API', function () {
                         partials: {
                         partials: {
                             test: '<a>{{a}}</a><p>{{b}}</p>'
                             test: '<a>{{a}}</a><p>{{b}}</p>'
                         },
                         },
-                        data: {
+                        scope: {
                             a: 'hi',
                             a: 'hi',
                             b: 'ho'
                             b: 'ho'
                         }
                         }

+ 1 - 1
test/unit/specs/viewmodel.js

@@ -19,7 +19,7 @@ describe('UNIT: ViewModel', function () {
         arr = [1, 2, 3],
         arr = [1, 2, 3],
         vm = new Seed({
         vm = new Seed({
             el: '#vm-test',
             el: '#vm-test',
-            data: {
+            scope: {
                 a: data,
                 a: data,
                 b: arr
                 b: arr
             }
             }