Skip to content

Instantly share code, notes, and snippets.

@ghempton
Created March 11, 2012 20:55
Show Gist options
  • Select an option

  • Save ghempton/2018185 to your computer and use it in GitHub Desktop.

Select an option

Save ghempton/2018185 to your computer and use it in GitHub Desktop.

Revisions

  1. ghempton revised this gist Aug 15, 2012. 1 changed file with 9 additions and 9 deletions.
    18 changes: 9 additions & 9 deletions bound_helper.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    // This file contains utilities for creating bound helpers

    // For reference: https://github.com/wagenet/ember.js/blob/ac66dcb8a1cbe91d736074441f853e0da474ee6e/packages/ember-handlebars/lib/views/bound_property_view.js
    var BoundHelperView = Ember.View.extend(Ember._Metamorph, {
    Ember.Handlebars.BoundHelperView = Ember.View.extend(Ember._Metamorph, {

    context: null,
    options: null,
    @@ -10,8 +12,8 @@ var BoundHelperView = Ember.View.extend(Ember._Metamorph, {
    value: Ember.K,

    valueForRender: function() {
    var value = this.value(Ember.getPath(this.context, this.property), this.options);
    if (this.options.escaped) { value = Handlebars.Utils.escapeExpression(value); }
    var value = this.value(Ember.Handlebars.getPath(this.context, this.property, this.options), this.options.hash);
    if (this.options.hash.escaped) { value = Handlebars.Utils.escapeExpression(value); }
    return value;
    },

    @@ -46,23 +48,21 @@ var BoundHelperView = Ember.View.extend(Ember._Metamorph, {

    });

    Ember.registerBoundHelper = function(name, func) {
    Ember.Handlebars.registerBoundHelper = function(name, func) {
    var propertyPaths = Array.prototype.slice.call(arguments, 2);
    Ember.Handlebars.registerHelper(name, function(property, options) {
    var data = options.data,
    view = data.view,
    ctx = this;

    var bindView = view.createChildView(BoundHelperView, {
    var bindView = view.createChildView(GT.Handlebars.BoundHelperView, {
    property: property,
    propertyPaths: propertyPaths,
    context: ctx,
    options: options.hash,
    options: options,
    value: func
    });

    view.appendChild(bindView);
    });
    };


    };
  2. ghempton revised this gist May 14, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion bound_helper.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    // For reference: https://github.com/wagenet/ember.js/blob/ac66dcb8a1cbe91d736074441f853e0da474ee6e/packages/ember-handlebars/lib/views/bound_property_view.js
    var BoundHelperView = Ember.View.extend(Ember.Metamorph, {
    var BoundHelperView = Ember.View.extend(Ember._Metamorph, {

    context: null,
    options: null,
  3. ghempton renamed this gist Mar 11, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. ghempton created this gist Mar 11, 2012.
    68 changes: 68 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    // For reference: https://github.com/wagenet/ember.js/blob/ac66dcb8a1cbe91d736074441f853e0da474ee6e/packages/ember-handlebars/lib/views/bound_property_view.js
    var BoundHelperView = Ember.View.extend(Ember.Metamorph, {

    context: null,
    options: null,
    property: null,
    // paths of the property that are also observed
    propertyPaths: [],

    value: Ember.K,

    valueForRender: function() {
    var value = this.value(Ember.getPath(this.context, this.property), this.options);
    if (this.options.escaped) { value = Handlebars.Utils.escapeExpression(value); }
    return value;
    },

    render: function(buffer) {
    buffer.push(this.valueForRender());
    },

    valueDidChange: function() {
    if (this.morph.isRemoved()) { return; }
    this.morph.html(this.valueForRender());
    },

    didInsertElement: function() {
    this.valueDidChange();
    },

    init: function() {
    this._super();
    Ember.addObserver(this.context, this.property, this, 'valueDidChange');
    this.get('propertyPaths').forEach(function(propName) {
    Ember.addObserver(this.context, this.property + '.' + propName, this, 'valueDidChange');
    }, this);
    },

    destroy: function() {
    Ember.removeObserver(this.context, this.property, this, 'valueDidChange');
    this.get('propertyPaths').forEach(function(propName) {
    this.context.removeObserver(this.property + '.' + propName, this, 'valueDidChange');
    }, this);
    this._super();
    }

    });

    Ember.registerBoundHelper = function(name, func) {
    var propertyPaths = Array.prototype.slice.call(arguments, 2);
    Ember.Handlebars.registerHelper(name, function(property, options) {
    var data = options.data,
    view = data.view,
    ctx = this;

    var bindView = view.createChildView(BoundHelperView, {
    property: property,
    propertyPaths: propertyPaths,
    context: ctx,
    options: options.hash,
    value: func
    });

    view.appendChild(bindView);
    });
    };