/
binding.js
663 lines (527 loc) · 21 KB
/
binding.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
require('ember-handlebars/ext');
require('ember-handlebars/views/handlebars_bound_view');
require('ember-handlebars/views/metamorph_view');
/**
@module ember
@submodule ember-handlebars
*/
var get = Ember.get, set = Ember.set, fmt = Ember.String.fmt;
var handlebarsGet = Ember.Handlebars.get, normalizePath = Ember.Handlebars.normalizePath;
var forEach = Ember.ArrayPolyfills.forEach;
var EmberHandlebars = Ember.Handlebars, helpers = EmberHandlebars.helpers;
// Binds a property into the DOM. This will create a hook in DOM that the
// KVO system will look for and update if the property changes.
function bind(property, options, preserveContext, shouldDisplay, valueNormalizer) {
var data = options.data,
fn = options.fn,
inverse = options.inverse,
view = data.view,
currentContext = this,
pathRoot, path, normalized,
observer;
normalized = normalizePath(currentContext, property, data);
pathRoot = normalized.root;
path = normalized.path;
// Set up observers for observable objects
if ('object' === typeof this) {
if (data.insideGroup) {
observer = function() {
Ember.run.once(view, 'rerender');
};
var template, context, result = handlebarsGet(pathRoot, path, options);
result = valueNormalizer(result);
context = preserveContext ? currentContext : result;
if (shouldDisplay(result)) {
template = fn;
} else if (inverse) {
template = inverse;
}
template(context, { data: options.data });
} else {
// Create the view that will wrap the output of this template/property
// and add it to the nearest view's childViews array.
// See the documentation of Ember._HandlebarsBoundView for more.
var bindView = view.createChildView(Ember._HandlebarsBoundView, {
preserveContext: preserveContext,
shouldDisplayFunc: shouldDisplay,
valueNormalizerFunc: valueNormalizer,
displayTemplate: fn,
inverseTemplate: inverse,
path: path,
pathRoot: pathRoot,
previousContext: currentContext,
isEscaped: !options.hash.unescaped,
templateData: options.data
});
view.appendChild(bindView);
observer = function() {
Ember.run.scheduleOnce('render', bindView, 'rerenderIfNeeded');
};
}
// Observes the given property on the context and
// tells the Ember._HandlebarsBoundView to re-render. If property
// is an empty string, we are printing the current context
// object ({{this}}) so updating it is not our responsibility.
if (path !== '') {
Ember.addObserver(pathRoot, path, observer);
view.one('willClearRender', function() {
Ember.removeObserver(pathRoot, path, observer);
});
}
} else {
// The object is not observable, so just render it out and
// be done with it.
data.buffer.push(handlebarsGet(pathRoot, path, options));
}
}
function simpleBind(property, options) {
var data = options.data,
view = data.view,
currentContext = this,
pathRoot, path, normalized,
observer;
normalized = normalizePath(currentContext, property, data);
pathRoot = normalized.root;
path = normalized.path;
// Set up observers for observable objects
if ('object' === typeof this) {
if (data.insideGroup) {
observer = function() {
Ember.run.once(view, 'rerender');
};
var result = handlebarsGet(pathRoot, path, options);
if (result === null || result === undefined) { result = ""; }
data.buffer.push(result);
} else {
var bindView = new Ember._SimpleHandlebarsView(
path, pathRoot, !options.hash.unescaped, options.data
);
bindView._parentView = view;
view.appendChild(bindView);
observer = function() {
Ember.run.scheduleOnce('render', bindView, 'rerender');
};
}
// Observes the given property on the context and
// tells the Ember._HandlebarsBoundView to re-render. If property
// is an empty string, we are printing the current context
// object ({{this}}) so updating it is not our responsibility.
if (path !== '') {
Ember.addObserver(pathRoot, path, observer);
view.one('willClearRender', function() {
Ember.removeObserver(pathRoot, path, observer);
});
}
} else {
// The object is not observable, so just render it out and
// be done with it.
data.buffer.push(handlebarsGet(pathRoot, path, options));
}
}
/**
@private
'_triageMustache' is used internally select between a binding and helper for
the given context. Until this point, it would be hard to determine if the
mustache is a property reference or a regular helper reference. This triage
helper resolves that.
This would not be typically invoked by directly.
@method _triageMustache
@for Ember.Handlebars.helpers
@param {String} property Property/helperID to triage
@param {Function} fn Context to provide for rendering
@return {String} HTML string
*/
EmberHandlebars.registerHelper('_triageMustache', function(property, fn) {
Ember.assert("You cannot pass more than one argument to the _triageMustache helper", arguments.length <= 2);
if (helpers[property]) {
return helpers[property].call(this, fn);
}
else {
return helpers.bind.apply(this, arguments);
}
});
/**
@private
`bind` can be used to display a value, then update that value if it
changes. For example, if you wanted to print the `title` property of
`content`:
```handlebars
{{bind "content.title"}}
```
This will return the `title` property as a string, then create a new observer
at the specified path. If it changes, it will update the value in DOM. Note
that if you need to support IE7 and IE8 you must modify the model objects
properties using `Ember.get()` and `Ember.set()` for this to work as it
relies on Ember's KVO system. For all other browsers this will be handled for
you automatically.
@method bind
@for Ember.Handlebars.helpers
@param {String} property Property to bind
@param {Function} fn Context to provide for rendering
@return {String} HTML string
*/
EmberHandlebars.registerHelper('bind', function(property, options) {
Ember.assert("You cannot pass more than one argument to the bind helper", arguments.length <= 2);
var context = (options.contexts && options.contexts[0]) || this;
if (!options.fn) {
return simpleBind.call(context, property, options);
}
return bind.call(context, property, options, false, function(result) {
return !Ember.isNone(result);
});
});
/**
@private
Use the `boundIf` helper to create a conditional that re-evaluates
whenever the truthiness of the bound value changes.
```handlebars
{{#boundIf "content.shouldDisplayTitle"}}
{{content.title}}
{{/boundIf}}
```
@method boundIf
@for Ember.Handlebars.helpers
@param {String} property Property to bind
@param {Function} fn Context to provide for rendering
@return {String} HTML string
*/
EmberHandlebars.registerHelper('boundIf', function(property, fn) {
var context = (fn.contexts && fn.contexts[0]) || this;
var func = function(result) {
if (Ember.typeOf(result) === 'array') {
return get(result, 'length') !== 0;
} else {
return !!result;
}
};
return bind.call(context, property, fn, true, func, func);
});
/**
@method with
@for Ember.Handlebars.helpers
@param {Function} context
@param {Hash} options
@return {String} HTML string
*/
EmberHandlebars.registerHelper('with', function(context, options) {
if (arguments.length === 4) {
var keywordName, path, rootPath, normalized;
Ember.assert("If you pass more than one argument to the with helper, it must be in the form #with foo as bar", arguments[1] === "as");
options = arguments[3];
keywordName = arguments[2];
path = arguments[0];
Ember.assert("You must pass a block to the with helper", options.fn && options.fn !== Handlebars.VM.noop);
if (Ember.isGlobalPath(path)) {
Ember.bind(options.data.keywords, keywordName, path);
} else {
normalized = normalizePath(this, path, options.data);
path = normalized.path;
rootPath = normalized.root;
// This is a workaround for the fact that you cannot bind separate objects
// together. When we implement that functionality, we should use it here.
var contextKey = Ember.$.expando + Ember.guidFor(rootPath);
options.data.keywords[contextKey] = rootPath;
// if the path is '' ("this"), just bind directly to the current context
var contextPath = path ? contextKey + '.' + path : contextKey;
Ember.bind(options.data.keywords, keywordName, contextPath);
}
return bind.call(this, path, options, true, function(result) {
return !Ember.isNone(result);
});
} else {
Ember.assert("You must pass exactly one argument to the with helper", arguments.length === 2);
Ember.assert("You must pass a block to the with helper", options.fn && options.fn !== Handlebars.VM.noop);
return helpers.bind.call(options.contexts[0], context, options);
}
});
/**
See `boundIf`
@method if
@for Ember.Handlebars.helpers
@param {Function} context
@param {Hash} options
@return {String} HTML string
*/
EmberHandlebars.registerHelper('if', function(context, options) {
Ember.assert("You must pass exactly one argument to the if helper", arguments.length === 2);
Ember.assert("You must pass a block to the if helper", options.fn && options.fn !== Handlebars.VM.noop);
return helpers.boundIf.call(options.contexts[0], context, options);
});
/**
@method unless
@for Ember.Handlebars.helpers
@param {Function} context
@param {Hash} options
@return {String} HTML string
*/
EmberHandlebars.registerHelper('unless', function(context, options) {
Ember.assert("You must pass exactly one argument to the unless helper", arguments.length === 2);
Ember.assert("You must pass a block to the unless helper", options.fn && options.fn !== Handlebars.VM.noop);
var fn = options.fn, inverse = options.inverse;
options.fn = inverse;
options.inverse = fn;
return helpers.boundIf.call(options.contexts[0], context, options);
});
/**
`bindAttr` allows you to create a binding between DOM element attributes and
Ember objects. For example:
```handlebars
<img {{bindAttr src="imageUrl" alt="imageTitle"}}>
```
The above handlebars template will fill the `<img>`'s `src` attribute will
the value of the property referenced with `"imageUrl"` and its `alt`
attribute with the value of the property referenced with `"imageTitle"`.
If the rendering context of this template is the following object:
```javascript
{
imageUrl: 'http://lolcats.info/haz-a-funny',
imageTitle: 'A humorous image of a cat'
}
```
The resulting HTML output will be:
```html
<img src="http://lolcats.info/haz-a-funny" alt="A humorous image of a cat">
```
`bindAttr` cannot redeclare existing DOM element attributes. The use of `src`
in the following `bindAttr` example will be ignored and the hard coded value
of `src="/failwhale.gif"` will take precedence:
```handlebars
<img src="/failwhale.gif" {{bindAttr src="imageUrl" alt="imageTitle"}}>
```
### `bindAttr` and the `class` attribute
`bindAttr` supports a special syntax for handling a number of cases unique
to the `class` DOM element attribute. The `class` attribute combines
multiple discreet values into a single attribute as a space-delimited
list of strings. Each string can be:
* a string return value of an object's property.
* a boolean return value of an object's property
* a hard-coded value
A string return value works identically to other uses of `bindAttr`. The
return value of the property will become the value of the attribute. For
example, the following view and template:
```javascript
AView = Ember.View.extend({
someProperty: function(){
return "aValue";
}.property()
})
```
```handlebars
<img {{bindAttr class="view.someProperty}}>
```
Result in the following rendered output:
```html
<img class="aValue">
```
A boolean return value will insert a specified class name if the property
returns `true` and remove the class name if the property returns `false`.
A class name is provided via the syntax
`somePropertyName:class-name-if-true`.
```javascript
AView = Ember.View.extend({
someBool: true
})
```
```handlebars
<img {{bindAttr class="view.someBool:class-name-if-true"}}>
```
Result in the following rendered output:
```html
<img class="class-name-if-true">
```
An additional section of the binding can be provided if you want to
replace the existing class instead of removing it when the boolean
value changes:
```handlebars
<img {{bindAttr class="view.someBool:class-name-if-true:class-name-if-false"}}>
```
A hard-coded value can be used by prepending `:` to the desired
class name: `:class-name-to-always-apply`.
```handlebars
<img {{bindAttr class=":class-name-to-always-apply"}}>
```
Results in the following rendered output:
```html
<img class=":class-name-to-always-apply">
```
All three strategies - string return value, boolean return value, and
hard-coded value – can be combined in a single declaration:
```handlebars
<img {{bindAttr class=":class-name-to-always-apply view.someBool:class-name-if-true view.someProperty"}}>
```
@method bindAttr
@for Ember.Handlebars.helpers
@param {Hash} options
@return {String} HTML string
*/
EmberHandlebars.registerHelper('bindAttr', function(options) {
var attrs = options.hash;
Ember.assert("You must specify at least one hash argument to bindAttr", !!Ember.keys(attrs).length);
var view = options.data.view;
var ret = [];
var ctx = this;
// Generate a unique id for this element. This will be added as a
// data attribute to the element so it can be looked up when
// the bound property changes.
var dataId = ++Ember.uuid;
// Handle classes differently, as we can bind multiple classes
var classBindings = attrs['class'];
if (classBindings !== null && classBindings !== undefined) {
var classResults = EmberHandlebars.bindClasses(this, classBindings, view, dataId, options);
ret.push('class="' + Handlebars.Utils.escapeExpression(classResults.join(' ')) + '"');
delete attrs['class'];
}
var attrKeys = Ember.keys(attrs);
// For each attribute passed, create an observer and emit the
// current value of the property as an attribute.
forEach.call(attrKeys, function(attr) {
var path = attrs[attr],
pathRoot, normalized;
Ember.assert(fmt("You must provide a String for a bound attribute, not %@", [path]), typeof path === 'string');
normalized = normalizePath(ctx, path, options.data);
pathRoot = normalized.root;
path = normalized.path;
var value = (path === 'this') ? pathRoot : handlebarsGet(pathRoot, path, options),
type = Ember.typeOf(value);
Ember.assert(fmt("Attributes must be numbers, strings or booleans, not %@", [value]), value === null || value === undefined || type === 'number' || type === 'string' || type === 'boolean');
var observer, invoker;
observer = function observer() {
var result = handlebarsGet(pathRoot, path, options);
Ember.assert(fmt("Attributes must be numbers, strings or booleans, not %@", [result]), result === null || result === undefined || typeof result === 'number' || typeof result === 'string' || typeof result === 'boolean');
var elem = view.$("[data-bindattr-" + dataId + "='" + dataId + "']");
// If we aren't able to find the element, it means the element
// to which we were bound has been removed from the view.
// In that case, we can assume the template has been re-rendered
// and we need to clean up the observer.
if (!elem || elem.length === 0) {
Ember.removeObserver(pathRoot, path, invoker);
return;
}
Ember.View.applyAttributeBindings(elem, attr, result);
};
invoker = function() {
Ember.run.scheduleOnce('render', observer);
};
// Add an observer to the view for when the property changes.
// When the observer fires, find the element using the
// unique data id and update the attribute to the new value.
if (path !== 'this') {
Ember.addObserver(pathRoot, path, invoker);
view.one('willClearRender', function() {
Ember.removeObserver(pathRoot, path, invoker);
});
}
// if this changes, also change the logic in ember-views/lib/views/view.js
if ((type === 'string' || (type === 'number' && !isNaN(value)))) {
ret.push(attr + '="' + Handlebars.Utils.escapeExpression(value) + '"');
} else if (value && type === 'boolean') {
// The developer controls the attr name, so it should always be safe
ret.push(attr + '="' + attr + '"');
}
}, this);
// Add the unique identifier
// NOTE: We use all lower-case since Firefox has problems with mixed case in SVG
ret.push('data-bindattr-' + dataId + '="' + dataId + '"');
return new EmberHandlebars.SafeString(ret.join(' '));
});
/**
@private
Helper that, given a space-separated string of property paths and a context,
returns an array of class names. Calling this method also has the side
effect of setting up observers at those property paths, such that if they
change, the correct class name will be reapplied to the DOM element.
For example, if you pass the string "fooBar", it will first look up the
"fooBar" value of the context. If that value is true, it will add the
"foo-bar" class to the current element (i.e., the dasherized form of
"fooBar"). If the value is a string, it will add that string as the class.
Otherwise, it will not add any new class name.
@method bindClasses
@for Ember.Handlebars
@param {Ember.Object} context The context from which to lookup properties
@param {String} classBindings A string, space-separated, of class bindings
to use
@param {Ember.View} view The view in which observers should look for the
element to update
@param {Srting} bindAttrId Optional bindAttr id used to lookup elements
@return {Array} An array of class names to add
*/
EmberHandlebars.bindClasses = function(context, classBindings, view, bindAttrId, options) {
var ret = [], newClass, value, elem;
// Helper method to retrieve the property from the context and
// determine which class string to return, based on whether it is
// a Boolean or not.
var classStringForPath = function(root, parsedPath, options) {
var val,
path = parsedPath.path;
if (path === 'this') {
val = root;
} else if (path === '') {
val = true;
} else {
val = handlebarsGet(root, path, options);
}
return Ember.View._classStringForValue(path, val, parsedPath.className, parsedPath.falsyClassName);
};
// For each property passed, loop through and setup
// an observer.
forEach.call(classBindings.split(' '), function(binding) {
// Variable in which the old class value is saved. The observer function
// closes over this variable, so it knows which string to remove when
// the property changes.
var oldClass;
var observer, invoker;
var parsedPath = Ember.View._parsePropertyPath(binding),
path = parsedPath.path,
pathRoot = context,
normalized;
if (path !== '' && path !== 'this') {
normalized = normalizePath(context, path, options.data);
pathRoot = normalized.root;
path = normalized.path;
}
// Set up an observer on the context. If the property changes, toggle the
// class name.
observer = function() {
// Get the current value of the property
newClass = classStringForPath(pathRoot, parsedPath, options);
elem = bindAttrId ? view.$("[data-bindattr-" + bindAttrId + "='" + bindAttrId + "']") : view.$();
// If we can't find the element anymore, a parent template has been
// re-rendered and we've been nuked. Remove the observer.
if (!elem || elem.length === 0) {
Ember.removeObserver(pathRoot, path, invoker);
} else {
// If we had previously added a class to the element, remove it.
if (oldClass) {
elem.removeClass(oldClass);
}
// If necessary, add a new class. Make sure we keep track of it so
// it can be removed in the future.
if (newClass) {
elem.addClass(newClass);
oldClass = newClass;
} else {
oldClass = null;
}
}
};
invoker = function() {
Ember.run.scheduleOnce('render', observer);
};
if (path !== '' && path !== 'this') {
Ember.addObserver(pathRoot, path, invoker);
view.one('willClearRender', function() {
Ember.removeObserver(pathRoot, path, invoker);
});
}
// We've already setup the observer; now we just need to figure out the
// correct behavior right now on the first pass through.
value = classStringForPath(pathRoot, parsedPath, options);
if (value) {
ret.push(value);
// Make sure we save the current value so that it can be removed if the
// observer fires.
oldClass = value;
}
});
return ret;
};