forked from charlesjolley/node-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlebars.js
3612 lines (2815 loc) · 97.7 KB
/
handlebars.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
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// This file is automatically generated. any changes will be lost
//
var Handlebars = require("handlebars");
require("./views");
require("./metamorph");
Ember.imports.Handlebars = Ember.imports.Handlebars || Handlebars;
(function() {
/**
@module ember
@submodule ember-handlebars
*/
var objectCreate = Ember.create;
var Handlebars = Ember.imports.Handlebars;
Ember.assert("Ember Handlebars requires Handlebars 1.0.beta.5 or greater", Handlebars && Handlebars.VERSION.match(/^1\.0\.beta\.[56789]$|^1\.0\.rc\.[123456789]+/));
/**
Prepares the Handlebars templating library for use inside Ember's view
system.
The Ember.Handlebars object is the standard Handlebars library, extended to use
Ember's get() method instead of direct property access, which allows
computed properties to be used inside templates.
To create an Ember.Handlebars template, call Ember.Handlebars.compile(). This will
return a function that can be used by Ember.View for rendering.
@class Handlebars
@namespace Ember
*/
Ember.Handlebars = objectCreate(Handlebars);
/**
@class helpers
@namespace Ember.Handlebars
*/
Ember.Handlebars.helpers = objectCreate(Handlebars.helpers);
/**
Override the the opcode compiler and JavaScript compiler for Handlebars.
@class Compiler
@namespace Ember.Handlebars
@private
@constructor
*/
Ember.Handlebars.Compiler = function() {};
// Handlebars.Compiler doesn't exist in runtime-only
if (Handlebars.Compiler) {
Ember.Handlebars.Compiler.prototype = objectCreate(Handlebars.Compiler.prototype);
}
Ember.Handlebars.Compiler.prototype.compiler = Ember.Handlebars.Compiler;
/**
@class JavaScriptCompiler
@namespace Ember.Handlebars
@private
@constructor
*/
Ember.Handlebars.JavaScriptCompiler = function() {};
// Handlebars.JavaScriptCompiler doesn't exist in runtime-only
if (Handlebars.JavaScriptCompiler) {
Ember.Handlebars.JavaScriptCompiler.prototype = objectCreate(Handlebars.JavaScriptCompiler.prototype);
Ember.Handlebars.JavaScriptCompiler.prototype.compiler = Ember.Handlebars.JavaScriptCompiler;
}
Ember.Handlebars.JavaScriptCompiler.prototype.namespace = "Ember.Handlebars";
Ember.Handlebars.JavaScriptCompiler.prototype.initializeBuffer = function() {
return "''";
};
/**
@private
Override the default buffer for Ember Handlebars. By default, Handlebars creates
an empty String at the beginning of each invocation and appends to it. Ember's
Handlebars overrides this to append to a single shared buffer.
@method appendToBuffer
@param string {String}
*/
Ember.Handlebars.JavaScriptCompiler.prototype.appendToBuffer = function(string) {
return "data.buffer.push("+string+");";
};
/**
@private
Rewrite simple mustaches from `{{foo}}` to `{{bind "foo"}}`. This means that all simple
mustaches in Ember's Handlebars will also set up an observer to keep the DOM
up to date when the underlying property changes.
@method mustache
@for Ember.Handlebars.Compiler
@param mustache
*/
Ember.Handlebars.Compiler.prototype.mustache = function(mustache) {
if (mustache.params.length || mustache.hash) {
return Handlebars.Compiler.prototype.mustache.call(this, mustache);
} else {
var id = new Handlebars.AST.IdNode(['_triageMustache']);
// Update the mustache node to include a hash value indicating whether the original node
// was escaped. This will allow us to properly escape values when the underlying value
// changes and we need to re-render the value.
if(!mustache.escaped) {
mustache.hash = mustache.hash || new Handlebars.AST.HashNode([]);
mustache.hash.pairs.push(["unescaped", new Handlebars.AST.StringNode("true")]);
}
mustache = new Handlebars.AST.MustacheNode([id].concat([mustache.id]), mustache.hash, !mustache.escaped);
return Handlebars.Compiler.prototype.mustache.call(this, mustache);
}
};
/**
Used for precompilation of Ember Handlebars templates. This will not be used during normal
app execution.
@method precompile
@for Ember.Handlebars
@static
@param {String} string The template to precompile
*/
Ember.Handlebars.precompile = function(string) {
var ast = Handlebars.parse(string);
var options = {
knownHelpers: {
action: true,
unbound: true,
bindAttr: true,
template: true,
view: true,
_triageMustache: true
},
data: true,
stringParams: true
};
var environment = new Ember.Handlebars.Compiler().compile(ast, options);
return new Ember.Handlebars.JavaScriptCompiler().compile(environment, options, undefined, true);
};
// We don't support this for Handlebars runtime-only
if (Handlebars.compile) {
/**
The entry point for Ember Handlebars. This replaces the default Handlebars.compile and turns on
template-local data and String parameters.
@method compile
@for Ember.Handlebars
@static
@param {String} string The template to compile
@return {Function}
*/
Ember.Handlebars.compile = function(string) {
var ast = Handlebars.parse(string);
var options = { data: true, stringParams: true };
var environment = new Ember.Handlebars.Compiler().compile(ast, options);
var templateSpec = new Ember.Handlebars.JavaScriptCompiler().compile(environment, options, undefined, true);
return Handlebars.template(templateSpec);
};
}
/**
@private
If a path starts with a reserved keyword, returns the root
that should be used.
@method normalizePath
@for Ember
@param root {Object}
@param path {String}
@param data {Hash}
*/
var normalizePath = Ember.Handlebars.normalizePath = function(root, path, data) {
var keywords = (data && data.keywords) || {},
keyword, isKeyword;
// Get the first segment of the path. For example, if the
// path is "foo.bar.baz", returns "foo".
keyword = path.split('.', 1)[0];
// Test to see if the first path is a keyword that has been
// passed along in the view's data hash. If so, we will treat
// that object as the new root.
if (keywords.hasOwnProperty(keyword)) {
// Look up the value in the template's data hash.
root = keywords[keyword];
isKeyword = true;
// Handle cases where the entire path is the reserved
// word. In that case, return the object itself.
if (path === keyword) {
path = '';
} else {
// Strip the keyword from the path and look up
// the remainder from the newly found root.
path = path.substr(keyword.length+1);
}
}
return { root: root, path: path, isKeyword: isKeyword };
};
/**
Lookup both on root and on window. If the path starts with
a keyword, the corresponding object will be looked up in the
template's data hash and used to resolve the path.
@method getPath
@for Ember.Handlebars
@param {Object} root The object to look up the property on
@param {String} path The path to be lookedup
@param {Object} options The template's option hash
*/
Ember.Handlebars.getPath = function(root, path, options) {
var data = options && options.data,
normalizedPath = normalizePath(root, path, data),
value;
// In cases where the path begins with a keyword, change the
// root to the value represented by that keyword, and ensure
// the path is relative to it.
root = normalizedPath.root;
path = normalizedPath.path;
value = Ember.get(root, path);
// If the path starts with a capital letter, look it up on Ember.lookup,
// which defaults to the `window` object in browsers.
if (value === undefined && root !== Ember.lookup && Ember.isGlobalPath(path)) {
value = Ember.get(Ember.lookup, path);
}
return value;
};
/**
@private
Registers a helper in Handlebars that will be called if no property with the
given name can be found on the current context object, and no helper with
that name is registered.
This throws an exception with a more helpful error message so the user can
track down where the problem is happening.
@method helperMissing
@for Ember.Handlebars.helpers
@param {String} path
@param {Hash} options
*/
Ember.Handlebars.registerHelper('helperMissing', function(path, options) {
var error, view = "";
error = "%@ Handlebars error: Could not find property '%@' on object %@.";
if (options.data){
view = options.data.view;
}
throw new Ember.Error(Ember.String.fmt(error, [view, path, this]));
});
})();
(function() {
/**
@method htmlSafe
@for Ember.String
@static
*/
Ember.String.htmlSafe = function(str) {
return new Handlebars.SafeString(str);
};
var htmlSafe = Ember.String.htmlSafe;
if (Ember.EXTEND_PROTOTYPES === true || Ember.EXTEND_PROTOTYPES.String) {
/**
See {{#crossLink "Ember.String/htmlSafe"}}{{/crossLink}}
@method htmlSafe
@for String
*/
String.prototype.htmlSafe = function() {
return htmlSafe(this);
};
}
})();
(function() {
/*jshint newcap:false*/
/**
@module ember
@submodule ember-handlebars
*/
var set = Ember.set, get = Ember.get;
// DOMManager should just abstract dom manipulation between jquery and metamorph
var DOMManager = {
remove: function(view) {
view.morph.remove();
},
prepend: function(view, html) {
view.morph.prepend(html);
},
after: function(view, html) {
view.morph.after(html);
},
html: function(view, html) {
view.morph.html(html);
},
// This is messed up.
replace: function(view) {
var morph = view.morph;
view.transitionTo('preRender');
view.clearRenderedChildren();
var buffer = view.renderToBuffer();
Ember.run.schedule('render', this, function() {
if (get(view, 'isDestroyed')) { return; }
view.invalidateRecursively('element');
view._notifyWillInsertElement();
morph.replaceWith(buffer.string());
view.transitionTo('inDOM');
view._notifyDidInsertElement();
});
},
empty: function(view) {
view.morph.html("");
}
};
// The `morph` and `outerHTML` properties are internal only
// and not observable.
/**
@class _Metamorph
@namespace Ember
@extends Ember.Mixin
@private
*/
Ember._Metamorph = Ember.Mixin.create({
isVirtual: true,
tagName: '',
instrumentName: 'render.metamorph',
init: function() {
this._super();
this.morph = Metamorph();
},
beforeRender: function(buffer) {
buffer.push(this.morph.startTag());
},
afterRender: function(buffer) {
buffer.push(this.morph.endTag());
},
createElement: function() {
var buffer = this.renderToBuffer();
this.outerHTML = buffer.string();
this.clearBuffer();
},
domManager: DOMManager
});
/**
@class _MetamorphView
@namespace Ember
@extends Ember.View
@uses Ember._Metamorph
@private
*/
Ember._MetamorphView = Ember.View.extend(Ember._Metamorph);
/**
@class _SimpleMetamorphView
@namespace Ember
@extends Ember.View
@uses Ember._Metamorph
@private
*/
Ember._SimpleMetamorphView = Ember.CoreView.extend(Ember._Metamorph);
})();
(function() {
/*globals Handlebars */
/**
@module ember
@submodule ember-handlebars
*/
var get = Ember.get, set = Ember.set, getPath = Ember.Handlebars.getPath;
Ember._SimpleHandlebarsView = Ember._SimpleMetamorphView.extend({
instrumentName: 'render.simpleHandlebars',
normalizedValue: Ember.computed(function() {
var path = get(this, 'path'),
pathRoot = get(this, 'pathRoot'),
result, templateData;
// Use the pathRoot as the result if no path is provided. This
// happens if the path is `this`, which gets normalized into
// a `pathRoot` of the current Handlebars context and a path
// of `''`.
if (path === '') {
result = pathRoot;
} else {
templateData = get(this, 'templateData');
result = getPath(pathRoot, path, { data: templateData });
}
return result;
}).property('path', 'pathRoot').volatile(),
render: function(buffer) {
// If not invoked via a triple-mustache ({{{foo}}}), escape
// the content of the template.
var escape = get(this, 'isEscaped');
var result = get(this, 'normalizedValue');
if (result === null || result === undefined) {
result = "";
} else if (!(result instanceof Handlebars.SafeString)) {
result = String(result);
}
if (escape) { result = Handlebars.Utils.escapeExpression(result); }
buffer.push(result);
return;
},
rerender: function() {
switch(this.state) {
case 'preRender':
case 'destroyed':
break;
case 'inBuffer':
throw new Error("Something you did tried to replace an {{expression}} before it was inserted into the DOM.");
case 'hasElement':
case 'inDOM':
this.domManager.replace(this);
break;
}
return this;
},
transitionTo: function(state) {
this.state = state;
}
});
/**
Ember._HandlebarsBoundView is a private view created by the Handlebars `{{bind}}`
helpers that is used to keep track of bound properties.
Every time a property is bound using a `{{mustache}}`, an anonymous subclass
of Ember._HandlebarsBoundView is created with the appropriate sub-template and
context set up. When the associated property changes, just the template for
this view will re-render.
@class _HandlebarsBoundView
@namespace Ember
@extends Ember._MetamorphView
@private
*/
Ember._HandlebarsBoundView = Ember._MetamorphView.extend({
instrumentName: 'render.boundHandlebars',
/**
The function used to determine if the `displayTemplate` or
`inverseTemplate` should be rendered. This should be a function that takes
a value and returns a Boolean.
@property shouldDisplayFunc
@type Function
@default null
*/
shouldDisplayFunc: null,
/**
Whether the template rendered by this view gets passed the context object
of its parent template, or gets passed the value of retrieving `path`
from the `pathRoot`.
For example, this is true when using the `{{#if}}` helper, because the
template inside the helper should look up properties relative to the same
object as outside the block. This would be false when used with `{{#with
foo}}` because the template should receive the object found by evaluating
`foo`.
@property preserveContext
@type Boolean
@default false
*/
preserveContext: false,
/**
If `preserveContext` is true, this is the object that will be used
to render the template.
@property previousContext
@type Object
*/
previousContext: null,
/**
The template to render when `shouldDisplayFunc` evaluates to true.
@property displayTemplate
@type Function
@default null
*/
displayTemplate: null,
/**
The template to render when `shouldDisplayFunc` evaluates to false.
@property inverseTemplate
@type Function
@default null
*/
inverseTemplate: null,
/**
The path to look up on `pathRoot` that is passed to
`shouldDisplayFunc` to determine which template to render.
In addition, if `preserveContext` is false, the object at this path will
be passed to the template when rendering.
@property path
@type String
@default null
*/
path: null,
/**
The object from which the `path` will be looked up. Sometimes this is the
same as the `previousContext`, but in cases where this view has been generated
for paths that start with a keyword such as `view` or `controller`, the
path root will be that resolved object.
@property pathRoot
@type Object
*/
pathRoot: null,
normalizedValue: Ember.computed(function() {
var path = get(this, 'path'),
pathRoot = get(this, 'pathRoot'),
valueNormalizer = get(this, 'valueNormalizerFunc'),
result, templateData;
// Use the pathRoot as the result if no path is provided. This
// happens if the path is `this`, which gets normalized into
// a `pathRoot` of the current Handlebars context and a path
// of `''`.
if (path === '') {
result = pathRoot;
} else {
templateData = get(this, 'templateData');
result = getPath(pathRoot, path, { data: templateData });
}
return valueNormalizer ? valueNormalizer(result) : result;
}).property('path', 'pathRoot', 'valueNormalizerFunc').volatile(),
rerenderIfNeeded: function() {
if (!get(this, 'isDestroyed') && get(this, 'normalizedValue') !== this._lastNormalizedValue) {
this.rerender();
}
},
/**
Determines which template to invoke, sets up the correct state based on
that logic, then invokes the default Ember.View `render` implementation.
This method will first look up the `path` key on `pathRoot`,
then pass that value to the `shouldDisplayFunc` function. If that returns
true, the `displayTemplate` function will be rendered to DOM. Otherwise,
`inverseTemplate`, if specified, will be rendered.
For example, if this Ember._HandlebarsBoundView represented the `{{#with foo}}`
helper, it would look up the `foo` property of its context, and
`shouldDisplayFunc` would always return true. The object found by looking
up `foo` would be passed to `displayTemplate`.
@method render
@param {Ember.RenderBuffer} buffer
*/
render: function(buffer) {
// If not invoked via a triple-mustache ({{{foo}}}), escape
// the content of the template.
var escape = get(this, 'isEscaped');
var shouldDisplay = get(this, 'shouldDisplayFunc'),
preserveContext = get(this, 'preserveContext'),
context = get(this, 'previousContext');
var inverseTemplate = get(this, 'inverseTemplate'),
displayTemplate = get(this, 'displayTemplate');
var result = get(this, 'normalizedValue');
this._lastNormalizedValue = result;
// First, test the conditional to see if we should
// render the template or not.
if (shouldDisplay(result)) {
set(this, 'template', displayTemplate);
// If we are preserving the context (for example, if this
// is an #if block, call the template with the same object.
if (preserveContext) {
set(this, '_context', context);
} else {
// Otherwise, determine if this is a block bind or not.
// If so, pass the specified object to the template
if (displayTemplate) {
set(this, '_context', result);
} else {
// This is not a bind block, just push the result of the
// expression to the render context and return.
if (result === null || result === undefined) {
result = "";
} else if (!(result instanceof Handlebars.SafeString)) {
result = String(result);
}
if (escape) { result = Handlebars.Utils.escapeExpression(result); }
buffer.push(result);
return;
}
}
} else if (inverseTemplate) {
set(this, 'template', inverseTemplate);
if (preserveContext) {
set(this, '_context', context);
} else {
set(this, '_context', result);
}
} else {
set(this, 'template', function() { return ''; });
}
return this._super(buffer);
}
});
})();
(function() {
/**
@module ember
@submodule ember-handlebars
*/
var get = Ember.get, set = Ember.set, fmt = Ember.String.fmt;
var getPath = Ember.Handlebars.getPath, 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;
normalized = normalizePath(currentContext, property, data);
pathRoot = normalized.root;
path = normalized.path;
// Set up observers for observable objects
if ('object' === typeof this) {
// 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);
var 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(getPath(pathRoot, path, options));
}
}
function simpleBind(property, options) {
var data = options.data,
view = data.view,
currentContext = this,
pathRoot, path, normalized;
normalized = normalizePath(currentContext, property, data);
pathRoot = normalized.root;
path = normalized.path;
// Set up observers for observable objects
if ('object' === typeof this) {
var bindView = Ember._SimpleHandlebarsView.create().setProperties({
path: path,
pathRoot: pathRoot,
isEscaped: !options.hash.unescaped,
previousContext: currentContext,
templateData: options.data
});
view.createChildView(bindView);
view.appendChild(bindView);
var 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(getPath(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.none(result);
});
});
/**
@private
Use the `boundIf` helper to create a conditional that re-evaluates
whenever 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.none(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);
}
});
/**
@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"}}>
```
@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);