reboundjs / rebound

Automatic data binding for backbone using HTMLBars.
MIT License
43 stars 4 forks source link

Each Helper should Iterate over Models and Objects in Addition to Collections #69

Open amiller-gh opened 9 years ago

amiller-gh commented 9 years ago

Need to add tests too, but here is the code:

  helpers.each = function (params, hash, templates) {

    if (_.isNull(params[0]) || _.isUndefined(params[0])) {
      console.warn("Undefined value passed to each helper! Maybe try providing a default value?", params, hash);return null;
    }

    var key,
        value = params[0].isCollection ? params[0].models : (params[0].isModel ? params[0].attributes : params[0]); // Accepts collections, arrays, or models

    if ((!_.isArray(value) || value.length === 0) && (!_.isObject(value) || Object.keys(value).length === 0)) {
      if (templates.inverse && templates.inverse["yield"]) templates.inverse["yield"]();
    } else {
      for (key in value) {
        if (value.hasOwnProperty(key)) this.yieldItem(value[key].cid, [value[key], key]);
      }
    }
    return _.uniqueId("rand");
  };