Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

first passing specs and implementation copied from existing gist

  • Loading branch information...
commit b457638bd2d0440d1f501c8fed90fb795a22e575 1 parent 4e5dac2
Derick Bailey authored September 23, 2012
50  spec/javascripts/computedField.spec.js
... ...
@@ -1,22 +1,62 @@
1 1
 describe("computed field", function(){
2 2
 
3 3
   describe("when computation is done for a single field", function(){
  4
+    var model, fieldList;
  5
+
  6
+    var Model = Backbone.Model.extend({
  7
+      initialize: function(){
  8
+        Backbone.Compute(this);
  9
+      },
  10
+
  11
+      computedField: Backbone.Compute("computedField", "anotherField", function(fields){
  12
+        fieldList = fields;
  13
+        return fields.anotherField + "-bar"
  14
+      })
  15
+    });
  16
+
  17
+    beforeEach(function(){
  18
+      model = new Model({
  19
+        anotherField: "foo"
  20
+      });
  21
+    });
  22
+
4 23
     it("should provide the specified field in the computation function", function(){
5  
-      throw "not yet implemented";
  24
+      expect(fieldList).toHaveOwnProperty("anotherField");
6 25
     });
7 26
 
8 27
     it("should `set` the computed field on the model to the returned value", function(){
9  
-      throw "not yet implemented";
  28
+      expect(model.get("computedField")).toEqual("foo-bar");
10 29
     });
11 30
   });
12 31
 
13 32
   describe("when computation is done for an array of fields", function(){
14  
-    it("should provide the specified fields in the computation function", function(){
15  
-      throw "not yet implemented";
  33
+    var model, fieldList;
  34
+
  35
+    var Model = Backbone.Model.extend({
  36
+      initialize: function(){
  37
+        Backbone.Compute(this);
  38
+      },
  39
+
  40
+      computedField: Backbone.Compute("computedField", ["f1", "f2"], function(fields){
  41
+        fieldList = fields;
  42
+        return fields.f1 + "-" + fields.f2
  43
+      })
  44
+    });
  45
+
  46
+    beforeEach(function(){
  47
+      model = new Model({
  48
+        f1: "foo",
  49
+        f2: "bar"
  50
+      });
  51
+    });
  52
+
  53
+    it("should provide the specified field in the computation function", function(){
  54
+      expect(fieldList).toHaveOwnProperty("f1");
  55
+      expect(fieldList).toHaveOwnProperty("f2");
16 56
     });
17 57
 
18 58
     it("should `set` the computed field on the model to the returned value", function(){
19  
-      throw "not yet implemented";
  59
+      expect(model.get("computedField")).toEqual("foo-bar");
20 60
     });
21 61
   });
22 62
 
8  spec/javascripts/helpers/jasmineMatchers.js
... ...
@@ -0,0 +1,8 @@
  1
+beforeEach(function() {
  2
+  this.addMatchers({
  3
+    toHaveOwnProperty: function(expectedProperty) {
  4
+      var obj = this.actual;
  5
+      return obj.hasOwnProperty(expectedProperty);
  6
+    }
  7
+  });
  8
+});
51  src/backbone.compute.js
... ...
@@ -1,3 +1,54 @@
1 1
 Backbone.Compute = (function(Backbone, _){
2 2
 
  3
+  function initializeModel(obj){
  4
+    for(var field in obj){
  5
+      if (obj[field] && obj[field].computedField){
  6
+        obj[field].call(obj);
  7
+      }
  8
+    }
  9
+  }
  10
+
  11
+  function computeField(fieldName, fieldList, callback){
  12
+    fieldList = _.flatten([fieldList]);
  13
+    var length = fieldList.length;
  14
+
  15
+    var fireCallback = function(){
  16
+      var fields = {};
  17
+
  18
+      for (var i = 0; i<length; i++){
  19
+        var field = fieldList[i];
  20
+        fields[field] = this.get(field);
  21
+      }
  22
+
  23
+      var value = callback.call(this, fields);
  24
+      this.set(fieldName, value);
  25
+
  26
+      return value;
  27
+    }
  28
+
  29
+    var computedFunc = function(){
  30
+      var cb = _.bind(fireCallback, this);
  31
+
  32
+      for (var i = 0; i<length; i++){
  33
+        var field = fieldList[i];
  34
+        this.on("change:" + field, cb);
  35
+      }
  36
+
  37
+      return cb();
  38
+    }
  39
+
  40
+    computedFunc.computedField = true;
  41
+
  42
+    return computedFunc;
  43
+  };
  44
+
  45
+  var Compute = function(){
  46
+    if (arguments.length === 1){
  47
+      return initializeModel(arguments[0]);
  48
+    } else {
  49
+      return computeField.apply(null, arguments);
  50
+    }
  51
+  };
  52
+
  53
+  return Compute;
3 54
 })(Backbone, _);

0 notes on commit b457638

Please sign in to comment.
Something went wrong with that request. Please try again.