-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathbinding.js
963 lines (775 loc) · 29.6 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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
/**
@module ember
@submodule ember-handlebars
*/
import Ember from "ember-metal/core"; // Ember.assert, Ember.warn, uuid
// var emberAssert = Ember.assert, Ember.warn = Ember.warn;
import EmberHandlebars from "ember-handlebars-compiler";
import { get } from "ember-metal/property_get";
import { apply, uuid } from "ember-metal/utils";
import { fmt } from "ember-runtime/system/string";
import { create as o_create } from "ember-metal/platform";
import isNone from 'ember-metal/is_none';
import { forEach } from "ember-metal/array";
import View from "ember-views/views/view";
import run from "ember-metal/run_loop";
import { removeObserver } from "ember-metal/observer";
import { isGlobalPath } from "ember-metal/binding";
import { bind as emberBind } from "ember-metal/binding";
import jQuery from "ember-views/system/jquery";
import { isArray } from "ember-metal/utils";
import { getEscaped } from "ember-handlebars/ext";
import keys from "ember-metal/keys";
import Cache from "ember-metal/cache";
import {
_HandlebarsBoundView,
SimpleHandlebarsView
} from "ember-handlebars/views/handlebars_bound_view";
import {
normalizePath,
handlebarsGet,
getEscaped
} from "ember-handlebars/ext";
import {
guidFor,
typeOf
} from "ember-metal/utils";
var helpers = EmberHandlebars.helpers;
var SafeString = EmberHandlebars.SafeString;
function exists(value) {
return !isNone(value);
}
var WithView = _HandlebarsBoundView.extend({
init: function() {
var controller;
apply(this, this._super, arguments);
var keywords = this.templateData.keywords;
var keywordName = this.templateHash.keywordName;
var keywordPath = this.templateHash.keywordPath;
var controllerName = this.templateHash.controller;
var preserveContext = this.preserveContext;
if (controllerName) {
var previousContext = this.previousContext;
controller = this.container.lookupFactory('controller:'+controllerName).create({
parentController: previousContext,
target: previousContext
});
this._generatedController = controller;
if (!preserveContext) {
this.set('controller', controller);
this.valueNormalizerFunc = function(result) {
controller.set('model', result);
return controller;
};
} else {
var controllerPath = jQuery.expando + guidFor(controller);
keywords[controllerPath] = controller;
emberBind(keywords, controllerPath + '.model', keywordPath);
keywordPath = controllerPath;
}
}
if (preserveContext) {
emberBind(keywords, keywordName, keywordPath);
}
},
willDestroy: function() {
this._super();
if (this._generatedController) {
this._generatedController.destroy();
}
}
});
// 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, childProperties) {
var data = options.data;
var fn = options.fn;
var inverse = options.inverse;
var view = data.view;
var normalized, observer, i;
// we relied on the behavior of calling without
// context to mean this === window, but when running
// "use strict", it's possible for this to === undefined;
var currentContext = this || window;
normalized = normalizePath(currentContext, property, data);
// Set up observers for observable objects
if ('object' === typeof this) {
if (data.insideGroup) {
observer = function() {
while (view._contextView) {
view = view._contextView;
}
run.once(view, 'rerender');
};
var template, context;
var result = handlebarsGet(currentContext, property, options);
result = valueNormalizer ? valueNormalizer(result) : result;
context = preserveContext ? currentContext : result;
if (shouldDisplay(result)) {
template = fn;
} else if (inverse) {
template = inverse;
}
template(context, { data: options.data });
} else {
var viewClass = _HandlebarsBoundView;
var viewOptions = {
preserveContext: preserveContext,
shouldDisplayFunc: shouldDisplay,
valueNormalizerFunc: valueNormalizer,
displayTemplate: fn,
inverseTemplate: inverse,
path: property,
pathRoot: currentContext,
previousContext: currentContext,
isEscaped: !options.hash.unescaped,
templateData: options.data,
templateHash: options.hash,
helperName: options.helperName
};
if (options.isWithHelper) {
viewClass = WithView;
}
// 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(viewClass, viewOptions);
view.appendChild(bindView);
observer = function() {
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 (normalized.path !== '') {
view.registerObserver(normalized.root, normalized.path, observer);
if (childProperties) {
for (i=0; i<childProperties.length; i++) {
view.registerObserver(normalized.root, normalized.path+'.'+childProperties[i], observer);
}
}
}
} else {
// The object is not observable, so just render it out and
// be done with it.
data.buffer.push(getEscaped(currentContext, property, options));
}
}
function simpleBind(currentContext, property, options) {
var data = options.data;
var view = data.view;
var normalized, observer, pathRoot, output;
normalized = normalizePath(currentContext, property, data);
pathRoot = normalized.root;
// Set up observers for observable objects
if (pathRoot && ('object' === typeof pathRoot)) {
if (data.insideGroup) {
observer = function() {
while (view._contextView) {
view = view._contextView;
}
run.once(view, 'rerender');
};
output = getEscaped(currentContext, property, options);
data.buffer.push(output);
} else {
var bindView = new SimpleHandlebarsView(
property, currentContext, !options.hash.unescaped, options.data
);
bindView._parentView = view;
view.appendChild(bindView);
observer = function() {
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 (normalized.path !== '') {
view.registerObserver(normalized.root, normalized.path, observer);
}
} else {
// The object is not observable, so just render it out and
// be done with it.
output = getEscaped(currentContext, property, options);
data.buffer.push(output);
}
}
function shouldDisplayIfHelperContent(result) {
var truthy = result && get(result, 'isTruthy');
if (typeof truthy === 'boolean') { return truthy; }
if (isArray(result)) {
return get(result, 'length') !== 0;
} else {
return !!result;
}
}
/**
'_triageMustache' is used internally select between a binding, helper, or component 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.
@private
@method _triageMustache
@for Ember.Handlebars.helpers
@param {String} property Property/helperID to triage
@param {Object} options hash of template/rendering options
@return {String} HTML string
*/
function _triageMustacheHelper(property, options) {
Ember.assert("You cannot pass more than one argument to the _triageMustache helper", arguments.length <= 2);
var helper = EmberHandlebars.resolveHelper(options.data.view.container, property);
if (helper) {
return helper.call(this, options);
}
return helpers.bind.call(this, property, options);
}
export var ISNT_HELPER_CACHE = new Cache(1000, function(key) {
return key.indexOf('-') === -1;
});
/**
Used to lookup/resolve handlebars helpers. The lookup order is:
* Look for a registered helper
* If a dash exists in the name:
* Look for a helper registed in the container
* Use Ember.ComponentLookup to find an Ember.Component that resolves
to the given name
@private
@method resolveHelper
@param {Container} container
@param {String} name the name of the helper to lookup
@return {Handlebars Helper}
*/
function resolveHelper(container, name) {
if (helpers[name]) {
return helpers[name];
}
if (!container || ISNT_HELPER_CACHE.get(name)) {
return;
}
var helper = container.lookup('helper:' + name);
if (!helper) {
var componentLookup = container.lookup('component-lookup:main');
Ember.assert("Could not find 'component-lookup:main' on the provided container, which is necessary for performing component lookups", componentLookup);
var Component = componentLookup.lookupFactory(name, container);
if (Component) {
helper = EmberHandlebars.makeViewHelper(Component);
container.register('helper:' + name, helper);
}
}
return helper;
}
/**
`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.
@private
@method bind
@for Ember.Handlebars.helpers
@param {String} property Property to bind
@param {Function} fn Context to provide for rendering
@return {String} HTML string
*/
function bindHelper(property, options) {
Ember.assert("You cannot pass more than one argument to the bind helper", arguments.length <= 2);
var context = (options.contexts && options.contexts.length) ? options.contexts[0] : this;
if (!options.fn) {
return simpleBind(context, property, options);
}
options.helperName = 'bind';
return bind.call(context, property, options, false, exists);
}
/**
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}}
```
@private
@method boundIf
@for Ember.Handlebars.helpers
@param {String} property Property to bind
@param {Function} fn Context to provide for rendering
@return {String} HTML string
*/
function boundIfHelper(property, fn) {
var context = (fn.contexts && fn.contexts.length) ? fn.contexts[0] : this;
fn.helperName = fn.helperName || 'boundIf';
return bind.call(context, property, fn, true, shouldDisplayIfHelperContent, shouldDisplayIfHelperContent, ['isTruthy', 'length']);
}
/**
@private
Use the `unboundIf` helper to create a conditional that evaluates once.
```handlebars
{{#unboundIf "content.shouldDisplayTitle"}}
{{content.title}}
{{/unboundIf}}
```
@method unboundIf
@for Ember.Handlebars.helpers
@param {String} property Property to bind
@param {Function} fn Context to provide for rendering
@return {String} HTML string
@since 1.4.0
*/
function unboundIfHelper(property, fn) {
var context = (fn.contexts && fn.contexts.length) ? fn.contexts[0] : this;
var data = fn.data;
var template = fn.fn;
var inverse = fn.inverse;
var normalized, propertyValue;
normalized = normalizePath(context, property, data);
propertyValue = handlebarsGet(context, property, fn);
if (!shouldDisplayIfHelperContent(propertyValue)) {
template = inverse;
}
template(context, { data: data });
}
/**
Use the `{{with}}` helper when you want to scope context. Take the following code as an example:
```handlebars
<h5>{{user.name}}</h5>
<div class="role">
<h6>{{user.role.label}}</h6>
<span class="role-id">{{user.role.id}}</span>
<p class="role-desc">{{user.role.description}}</p>
</div>
```
`{{with}}` can be our best friend in these cases,
instead of writing `user.role.*` over and over, we use `{{#with user.role}}`.
Now the context within the `{{#with}} .. {{/with}}` block is `user.role` so you can do the following:
```handlebars
<h5>{{user.name}}</h5>
<div class="role">
{{#with user.role}}
<h6>{{label}}</h6>
<span class="role-id">{{id}}</span>
<p class="role-desc">{{description}}</p>
{{/with}}
</div>
```
### `as` operator
This operator aliases the scope to a new name. It's helpful for semantic clarity and to retain
default scope or to reference from another `{{with}}` block.
```handlebars
// posts might not be
{{#with user.posts as blogPosts}}
<div class="notice">
There are {{blogPosts.length}} blog posts written by {{user.name}}.
</div>
{{#each post in blogPosts}}
<li>{{post.title}}</li>
{{/each}}
{{/with}}
```
Without the `as` operator, it would be impossible to reference `user.name` in the example above.
NOTE: The alias should not reuse a name from the bound property path.
For example: `{{#with foo.bar as foo}}` is not supported because it attempts to alias using
the first part of the property path, `foo`. Instead, use `{{#with foo.bar as baz}}`.
### `controller` option
Adding `controller='something'` instructs the `{{with}}` helper to create and use an instance of
the specified controller with the new context as its content.
This is very similar to using an `itemController` option with the `{{each}}` helper.
```handlebars
{{#with users.posts controller='userBlogPosts'}}
{{!- The current context is wrapped in our controller instance }}
{{/with}}
```
In the above example, the template provided to the `{{with}}` block is now wrapped in the
`userBlogPost` controller, which provides a very elegant way to decorate the context with custom
functions/properties.
@method with
@for Ember.Handlebars.helpers
@param {Function} context
@param {Hash} options
@return {String} HTML string
*/
function withHelper(context, options) {
var bindContext, preserveContext;
var helperName = 'with';
if (arguments.length === 4) {
var keywordName, path, rootPath, normalized, contextPath;
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];
if (path) {
helperName += ' ' + path + ' as ' + keywordName;
}
Ember.assert("You must pass a block to the with helper", options.fn && options.fn !== Handlebars.VM.noop);
var localizedOptions = o_create(options);
localizedOptions.data = o_create(options.data);
localizedOptions.data.keywords = o_create(options.data.keywords || {});
if (isGlobalPath(path)) {
contextPath = 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 = jQuery.expando + guidFor(rootPath);
localizedOptions.data.keywords[contextKey] = rootPath;
// if the path is '' ("this"), just bind directly to the current context
contextPath = path ? contextKey + '.' + path : contextKey;
}
localizedOptions.hash.keywordName = keywordName;
localizedOptions.hash.keywordPath = contextPath;
bindContext = this;
context = contextPath;
options = localizedOptions;
preserveContext = true;
} 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);
helperName += ' ' + context;
bindContext = options.contexts[0];
preserveContext = false;
}
options.helperName = helperName;
options.isWithHelper = true;
return bind.call(bindContext, context, options, preserveContext, exists);
}
/**
See [boundIf](/api/classes/Ember.Handlebars.helpers.html#method_boundIf)
and [unboundIf](/api/classes/Ember.Handlebars.helpers.html#method_unboundIf)
@method if
@for Ember.Handlebars.helpers
@param {Function} context
@param {Hash} options
@return {String} HTML string
*/
function ifHelper(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);
options.helperName = options.helperName || ('if ' + context);
if (options.data.isUnbound) {
return helpers.unboundIf.call(options.contexts[0], context, options);
} else {
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
*/
function unlessHelper(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;
var inverse = options.inverse;
var helperName = 'unless';
if (context) {
helperName += ' ' + context;
}
options.fn = inverse;
options.inverse = fn;
options.helperName = options.helperName || helperName;
if (options.data.isUnbound) {
return helpers.unboundIf.call(options.contexts[0], context, options);
} else {
return helpers.boundIf.call(options.contexts[0], context, options);
}
}
/**
`bind-attr` allows you to create a binding between DOM element attributes and
Ember objects. For example:
```handlebars
<img {{bind-attr src="imageUrl" alt="imageTitle"}}>
```
The above handlebars template will fill the `<img>`'s `src` attribute with
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">
```
`bind-attr` cannot redeclare existing DOM element attributes. The use of `src`
in the following `bind-attr` example will be ignored and the hard coded value
of `src="/failwhale.gif"` will take precedence:
```handlebars
<img src="/failwhale.gif" {{bind-attr src="imageUrl" alt="imageTitle"}}>
```
### `bind-attr` and the `class` attribute
`bind-attr` supports a special syntax for handling a number of cases unique
to the `class` DOM element attribute. The `class` attribute combines
multiple discrete 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 `bind-attr`. The
return value of the property will become the value of the attribute. For
example, the following view and template:
```javascript
AView = View.extend({
someProperty: function() {
return "aValue";
}.property()
})
```
```handlebars
<img {{bind-attr 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 = View.extend({
someBool: true
})
```
```handlebars
<img {{bind-attr 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 {{bind-attr 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 {{bind-attr 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 {{bind-attr class=":class-name-to-always-apply view.someBool:class-name-if-true view.someProperty"}}>
```
@method bind-attr
@for Ember.Handlebars.helpers
@param {Hash} options
@return {String} HTML string
*/
function bindAttrHelper(options) {
var attrs = options.hash;
Ember.assert("You must specify at least one hash argument to bind-attr", !!keys(attrs).length);
var view = options.data.view;
var ret = [];
// we relied on the behavior of calling without
// context to mean this === window, but when running
// "use strict", it's possible for this to === undefined;
var ctx = this || window;
// 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 = uuid();
// Handle classes differently, as we can bind multiple classes
var classBindings = attrs['class'];
if (classBindings != null) {
var classResults = bindClasses(ctx, classBindings, view, dataId, options);
ret.push('class="' + Handlebars.Utils.escapeExpression(classResults.join(' ')) + '"');
delete attrs['class'];
}
var attrKeys = 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];
var normalized;
Ember.assert(fmt("You must provide an expression as the value of bound attribute. You specified: %@=%@", [attr, path]), typeof path === 'string');
normalized = normalizePath(ctx, path, options.data);
var value = (path === 'this') ? normalized.root : handlebarsGet(ctx, path, options);
var type = 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;
observer = function observer() {
var result = handlebarsGet(ctx, 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) {
removeObserver(normalized.root, normalized.path, observer);
return;
}
View.applyAttributeBindings(elem, attr, result);
};
// 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.
// Note: don't add observer when path is 'this' or path
// is whole keyword e.g. {{#each x in list}} ... {{bind-attr attr="x"}}
if (path !== 'this' && !(normalized.isKeyword && normalized.path === '' )) {
view.registerObserver(normalized.root, normalized.path, observer);
}
// 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 SafeString(ret.join(' '));
}
/**
See `bind-attr`
@method bindAttr
@for Ember.Handlebars.helpers
@deprecated
@param {Function} context
@param {Hash} options
@return {String} HTML string
*/
function bindAttrHelperDeprecated() {
Ember.warn("The 'bindAttr' view helper is deprecated in favor of 'bind-attr'");
return helpers['bind-attr'].apply(this, arguments);
}
/**
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.
@private
@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 {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
*/
function bindClasses(context, classBindings, view, bindAttrId, options) {
var ret = [];
var 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;
var path = parsedPath.path;
if (path === 'this') {
val = root;
} else if (path === '') {
val = true;
} else {
val = handlebarsGet(root, path, options);
}
return 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;
var parsedPath = View._parsePropertyPath(binding);
var path = parsedPath.path;
var pathRoot = context;
var 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(context, 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) {
removeObserver(pathRoot, path, observer);
} 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;
}
}
};
if (path !== '' && path !== 'this') {
view.registerObserver(pathRoot, path, observer);
}
// 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(context, 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;
}
export {
bind,
_triageMustacheHelper,
resolveHelper,
bindHelper,
boundIfHelper,
unboundIfHelper,
withHelper,
ifHelper,
unlessHelper,
bindAttrHelper,
bindAttrHelperDeprecated,
bindClasses
};