Skip to content

Commit 5ad8ede

Browse files
committed
change sd-viewmodel to sd-component, allow direct use of Objects in Seed.component()
1 parent 1be615f commit 5ad8ede

File tree

11 files changed

+61
-43
lines changed

11 files changed

+61
-43
lines changed

src/compiler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ CompilerProto.compile = function (node, root) {
211211
transClass = node.getAttribute(config.transClassAttr)
212212

213213
// we need to check for any possbile special directives
214-
// e.g. sd-repeat, sd-viewmodel & sd-partial
214+
// e.g. sd-repeat, sd-component & sd-partial
215215
if (repeatExp) { // repeat block
216216

217217
// repeat block cannot have sd-id at the same time.
@@ -224,7 +224,7 @@ CompilerProto.compile = function (node, root) {
224224
} else if (vmId && !root) { // child ViewModels
225225

226226
node.removeAttribute(config.vmAttr)
227-
var ChildVM = compiler.getOption('viewmodels', vmId)
227+
var ChildVM = compiler.getOption('components', vmId)
228228
if (ChildVM) {
229229
var child = new ChildVM({
230230
el: node,

src/directives/repeat.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ module.exports = {
9696
ViewModel = ViewModel || require('../viewmodel')
9797
var vmId = el.getAttribute(config.vmAttr)
9898
if (vmId) el.removeAttribute(config.vmAttr)
99-
self.ChildVM = self.compiler.getOption('viewmodels', vmId) || ViewModel
99+
self.ChildVM = self.compiler.getOption('components', vmId) || ViewModel
100100

101101
// extract transition information
102102
self.hasTransition = !!(

src/main.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ ViewModel.filter = function (id, fn) {
3636
/**
3737
* Allows user to register/retrieve a ViewModel constructor
3838
*/
39-
ViewModel.viewmodel = function (id, Ctor) {
40-
if (!Ctor) return utils.viewmodels[id]
41-
utils.viewmodels[id] = Ctor
39+
ViewModel.component = function (id, Ctor) {
40+
if (!Ctor) return utils.components[id]
41+
utils.components[id] = Ctor.prototype instanceof ViewModel
42+
? Ctor
43+
: ViewModel.extend(Ctor)
4244
return this
4345
}
4446

@@ -131,7 +133,7 @@ function inheritOptions (child, parent, topLevel) {
131133
function updatePrefix () {
132134
var prefix = config.prefix
133135
config.idAttr = prefix + '-id'
134-
config.vmAttr = prefix + '-viewmodel'
136+
config.vmAttr = prefix + '-component'
135137
config.preAttr = prefix + '-pre'
136138
config.textAttr = prefix + '-text'
137139
config.repeatAttr = prefix + '-repeat'

src/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var utils = module.exports = {
1717

1818
// global storage for user-registered
1919
// vms, partials and transitions
20-
viewmodels : makeHash(),
20+
components : makeHash(),
2121
partials : makeHash(),
2222
transitions : makeHash(),
2323

test/functional/fixtures/encapsulation.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
<div class="dir" sd-hola="dirMsg"></div>
1212
<div class="filter">{{filterMsg | nodigits}}</div>
1313
<div class="partial" sd-partial="partial-test"></div>
14-
<div class="vm" sd-viewmodel="vm-test">{{vmMsg}}</div>
14+
<div class="vm" sd-component="vm-test">{{vmMsg}}</div>
1515
</div>
1616
<script>
1717
var T = Seed.extend({
18-
viewmodels: {
18+
components: {
1919
'vm-test': Seed.extend({
2020
scope: {
21-
vmMsg: 'viewmodel works'
21+
vmMsg: 'component works'
2222
}
2323
})
2424
},

test/functional/fixtures/nested-viewmodels.html

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,23 @@
2424
<div id="grandpa" data-name="Andy" data-family="Johnson">
2525
<p class="ancestor">{{name}} {{family}}</p>
2626

27-
<div sd-viewmodel="man" data-name="Jack">
27+
<div sd-component="man" data-name="Jack">
2828
<p class="jack">{{name}}, son of {{^name}}</p>
2929

30-
<div sd-viewmodel="man" data-name="Mike">
30+
<div sd-component="man" data-name="Mike">
3131
<p class="mike">{{name}}, son of {{^name}}</p>
3232

33-
<div sd-viewmodel="offspring" data-name="Tim" class="tim">
33+
<div sd-component="offspring" data-name="Tim" class="tim">
3434
</div>
3535

36-
<div sd-viewmodel="offspring" data-name="Tom" class="tom">
36+
<div sd-component="offspring" data-name="Tom" class="tom">
3737
</div>
3838
</div>
3939

40-
<div sd-viewmodel="man" data-name="Jason">
40+
<div sd-component="man" data-name="Jason">
4141
<p class="jason">{{name}}, son of {{^name}}</p>
4242

43-
<div sd-viewmodel="offspring" data-name="Andrew" class="andrew">
43+
<div sd-component="offspring" data-name="Andrew" class="andrew">
4444
</div>
4545
</div>
4646
</div>
@@ -68,8 +68,8 @@
6868
})
6969

7070
Seed
71-
.viewmodel('man', Man)
72-
.viewmodel('offspring', Offspring)
71+
.component('man', Man)
72+
.component('offspring', Offspring)
7373

7474
new Man({
7575
el: '#grandpa'

test/functional/fixtures/repeated-vms.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
<script src="../../../dist/seed.js"></script>
88
</head>
99
<body>
10-
<div class="item" sd-repeat="item:items" sd-viewmodel="item" sd-on="click:click">
10+
<div class="item" sd-repeat="item:items" sd-component="item" sd-on="click:click">
1111
{{msg + ' ' + item.title}}
1212
</div>
1313
<script>
1414
Seed.config({ debug: true })
1515

16-
Seed.viewmodel('item', Seed.extend({
16+
var Item = Seed.extend({
1717
init: function () {
1818
this.item.title += ' init'
1919
},
@@ -25,7 +25,9 @@
2525
scope: {
2626
msg: 'msg'
2727
}
28-
}))
28+
})
29+
30+
Seed.component('item', Item)
2931

3032
new Seed({
3133
el: 'body',

test/functional/specs/encapsulation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ casper.test.begin('Component Encapsulation', 4, function (test) {
55
test.assertSelectorHasText('.dir', 'directive works')
66
test.assertSelectorHasText('.filter', 'filter works')
77
test.assertSelectorHasText('.partial', 'partial works')
8-
test.assertSelectorHasText('.vm', 'viewmodel works')
8+
test.assertSelectorHasText('.vm', 'component works')
99
})
1010
.run(function () {
1111
test.done()

test/unit/specs/api.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,30 +101,44 @@ describe('UNIT: API', function () {
101101

102102
})
103103

104-
describe('viewmodel()', function () {
104+
describe('component()', function () {
105105

106106
var testId = 'api-vm-test',
107-
Test = Seed.extend({
107+
testId2 = testId + '2',
108+
opts = {
108109
className: 'hihi',
109110
scope: { hi: 'ok' }
110-
}),
111+
},
112+
Test = Seed.extend(opts),
111113
utils = require('seed/src/utils')
112114

113-
it('should register a VM constructor', function () {
114-
Seed.viewmodel(testId, Test)
115-
assert.strictEqual(utils.viewmodels[testId], Test)
115+
it('should register a Component constructor', function () {
116+
Seed.component(testId, Test)
117+
assert.strictEqual(utils.components[testId], Test)
118+
})
119+
120+
it('should also work with option objects', function () {
121+
Seed.component(testId2, opts)
122+
assert.ok(utils.components[testId2].prototype instanceof Seed)
116123
})
117124

118125
it('should retrieve the VM if has only one arg', function () {
119-
assert.strictEqual(Seed.viewmodel(testId), Test)
126+
assert.strictEqual(Seed.component(testId), Test)
120127
})
121128

122-
it('should work with sd-viewmodel', function () {
123-
mock(testId, '<div sd-viewmodel="' + testId + '">{{hi}}</div>')
129+
it('should work with sd-component', function () {
130+
131+
mock(testId, '<div sd-component="' + testId + '">{{hi}}</div>')
124132
var t = new Seed({ el: '#' + testId }),
125133
child = t.$el.querySelector('div')
126134
assert.strictEqual(child.className, 'hihi')
127135
assert.strictEqual(child.textContent, 'ok')
136+
137+
mock(testId2, '<div sd-component="' + testId2 + '">{{hi}}</div>')
138+
var t2 = new Seed({ el: '#' + testId2 }),
139+
child2 = t2.$el.querySelector('div')
140+
assert.strictEqual(child2.className, 'hihi')
141+
assert.strictEqual(child2.textContent, 'ok')
128142
})
129143

130144
})
@@ -489,7 +503,7 @@ describe('UNIT: API', function () {
489503

490504
})
491505

492-
describe('viewmodels', function () {
506+
describe('components', function () {
493507

494508
it('should allow the VM to use private child VMs', function () {
495509
var Child = Seed.extend({
@@ -498,11 +512,11 @@ describe('UNIT: API', function () {
498512
}
499513
})
500514
var Parent = Seed.extend({
501-
template: '<p>{{name}}</p><div sd-viewmodel="child">{{name}}</div>',
515+
template: '<p>{{name}}</p><div sd-component="child">{{name}}</div>',
502516
scope: {
503517
name: 'dad'
504518
},
505-
viewmodels: {
519+
components: {
506520
child: Child
507521
}
508522
})

test/unit/specs/directives.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,8 @@ describe('UNIT: Directives', function () {
560560
}
561561
})
562562
var t = new Seed({
563-
template: '<div sd-viewmodel="child" sd-id="hihi"></div>',
564-
viewmodels: {
563+
template: '<div sd-component="child" sd-id="hihi"></div>',
564+
components: {
565565
child: Child
566566
}
567567
})

test/unit/specs/viewmodel.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ describe('UNIT: ViewModel', function () {
165165
}
166166
})
167167
var Test = Seed.extend({
168-
template: '<div sd-viewmodel="test"></div><div sd-viewmodel="test"></div>',
169-
viewmodels: {
168+
template: '<div sd-component="test"></div><div sd-component="test"></div>',
169+
components: {
170170
test: Child
171171
}
172172
})
@@ -195,8 +195,8 @@ describe('UNIT: ViewModel', function () {
195195
}
196196
})
197197
var Middle = Seed.extend({
198-
template: '<div sd-viewmodel="bottom"></div>',
199-
viewmodels: { bottom: Bottom },
198+
template: '<div sd-component="bottom"></div>',
199+
components: { bottom: Bottom },
200200
init: function () {
201201
this.$on('hello', function (m) {
202202
assert.strictEqual(m, msg)
@@ -205,8 +205,8 @@ describe('UNIT: ViewModel', function () {
205205
}
206206
})
207207
var Top = Seed.extend({
208-
template: '<div sd-viewmodel="middle"></div>',
209-
viewmodels: { middle: Middle },
208+
template: '<div sd-component="middle"></div>',
209+
components: { middle: Middle },
210210
init: function () {
211211
this.$on('hello', function (m) {
212212
assert.strictEqual(m, msg)

0 commit comments

Comments
 (0)