function mergeDOMViewBindings(view, props, hash) {
  Ember.assert(
    "Setting 'attributeBindings' via template helpers is not allowed. " +
    "Please subclass Ember.View and set it there instead.",
    !hash.attributeBindings
  );

  if (hash.id) {
    props.elementId = read(hash.id);
  }

  if (hash.tag) {
    props.tagName = read(hash.tag);
  }

  var classBindings = [];

  if (hash['class']) {
    if (typeof hash['class'] === 'string') {
      props.classNames = hash['class'].split(' ');
    } else {
      classBindings.push(hash['class']._label);
    }
  }

  if (hash.classBinding) {
    a_push.apply(classBindings, hash.classBinding.split(' '));
  }

  if (hash.classNameBindings) {
    a_push.apply(classBindings, hash.classNameBindings.split(' '));
  }

  if (classBindings.length > 0) {
    props.classNameBindings = classBindings;

    for (var i = 0; i < classBindings.length; i++) {
      var classBinding = streamifyClassNameBinding(view, classBindings[i]);
      if (isStream(classBinding)) {
        classBindings[i] = classBinding;
      } else {
        classBindings[i] = new SimpleStream(classBinding);
      }
    }
  }
}
Exemple #2
0
 viewOptions.classNameBindings = map(itemClassBindings, function(classBinding) {
   return streamifyClassNameBinding(view, classBinding);
 });
function mergeDOMViewBindings(view, props, hash) {
  Ember.assert(
    "Setting 'attributeBindings' via template helpers is not allowed. " +
    "Please subclass Ember.View and set it there instead.",
    !hash.attributeBindings
  );

  if (hash.id) {
    props.id = props.elementId = read(hash.id);
  }

  if (hash.tag) {
    props.tagName = read(hash.tag);
  }

  var classBindings = [];

  if (hash['class']) {
    if (typeof hash['class'] === 'string') {
      props.classNames = hash['class'].split(' ');
    } else if (hash['class']._label) {
      // label exists for via property paths in the template
      // but not for streams with nested sub-expressions
      classBindings.push(hash['class']._label);
    } else {
      // this stream did not have a label which means that
      // it is not a simple property path type stream (likely
      // the result of a sub-expression)
      classBindings.push(hash['class']);
    }
  }

  if (hash.classBinding) {
    a_push.apply(classBindings, hash.classBinding.split(' '));
  }

  if (hash.classNameBindings) {
    a_push.apply(classBindings, hash.classNameBindings.split(' '));
  }

  if (classBindings.length > 0) {
    props.classNameBindings = classBindings;

    for (var i = 0; i < classBindings.length; i++) {
      var initialValue = classBindings[i];
      var classBinding;

      if (isStream(initialValue)) {
        classBinding = initialValue;
      } else {
        classBinding = streamifyClassNameBinding(view, initialValue);
      }

      if (isStream(classBinding)) {
        classBindings[i] = classBinding;
      } else {
        classBindings[i] = new SimpleStream(classBinding);
      }
    }
  }
}
    forEach(classBindings, function(binding) {

      var boundBinding;
      if (isStream(binding)) {
        boundBinding = binding;
      } else {
        boundBinding = streamifyClassNameBinding(this, binding, '_view.');
      }

      // 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;

      // Set up an observer on the context. If the property changes, toggle the
      // class name.
      var observer = this._wrapAsScheduled(function() {
        // Get the current value of the property
        elem = this.$();
        newClass = read(boundBinding);

        // If we had previously added a class to the element, remove it.
        if (oldClass) {
          elem.removeClass(oldClass);
          // Also remove from classNames so that if the view gets rerendered,
          // the class doesn't get added back to the DOM.
          classNames.removeObject(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;
        }
      });

      // Get the class name for the property at its current value
      dasherizedClass = read(boundBinding);

      if (dasherizedClass) {
        // Ensure that it gets into the classNames array
        // so it is displayed when we render.
        addObject(classNames, dasherizedClass);

        // Save a reference to the class name so we can remove it
        // if the observer fires. Remember that this variable has
        // been closed over by the observer.
        oldClass = dasherizedClass;
      }

      subscribe(boundBinding, observer, this);
      // Remove className so when the view is rerendered,
      // the className is added based on binding reevaluation
      this.one('willClearRender', function() {
        if (oldClass) {
          classNames.removeObject(oldClass);
          oldClass = null;
        }
      });

    }, this);