Permalink
Browse files

making it easier to overwrite model behavior

  • Loading branch information...
justinbmeyer committed May 25, 2012
1 parent 251be5f commit 235097a46e45329d63da9b6d28a6c284c1b2a157
Showing with 114 additions and 30 deletions.
  1. +6 −6 control/control.html
  2. +32 −24 model/model.js
  3. +76 −0 model/model_test.js
View
@@ -39,18 +39,18 @@
<body>
<div id="demo-html">
<ul id="tabs" class="tabs ui-helper-clearfix">
- <li><a href="#tab1">Tab 1</a></li>
- <li><a href="#tab2">Tab 2</a></li>
- <li><a href="#tab3">Tab 3</a></li>
+ <li><a href="#tab1">Model</a></li>
+ <li><a href="#tab2">View</a></li>
+ <li><a href="#tab3">Controller</a></li>
</ul>
<div id="tab1" class="tab">
- Tab 1 Content
+ Model Content
</div>
<div id="tab2" class="tab">
- Tab 2 Content
+ View Content
</div>
<div id="tab3" class="tab">
- Tab 3 Content
+ Controller Content
</div>
</div>
<script type="text/javascript" src="../../steal/steal.js"></script>
View
@@ -96,7 +96,7 @@ steal('can/observe', function() {
/**
* @Static
*/
- ajaxMethods = {
+ //
/**
* @function bind
* `bind(eventType, handler(event, instance))` listens to
@@ -153,6 +153,7 @@ steal('can/observe', function() {
* id: "Id"
* },{});
*/
+ ajaxMethods = {
/**
* @function create
* `create(attributes) -> Deferred` is used by [can.Model::save save] to create a
@@ -172,7 +173,7 @@ steal('can/observe', function() {
* new Recipe({name: "hot dog"}).save();
*
*
- * ## Implmeent with a Function
+ * ## Implement with a Function
*
* You can also implement create by yourself. Create gets called
* with `attrs`, which are the [can.Observe::serialize serialized] model
@@ -543,30 +544,24 @@ steal('can/observe', function() {
if(this === can.Model){
return;
}
- var self = this;
-
+ var self = this,
+ clean = can.proxy(this._clean, self);
+
can.each(ajaxMethods, function(method, name){
if ( ! can.isFunction( self[name] )) {
self[name] = ajaxMaker(method, self[name]);
}
+ if (self["make"+can.capitalize(name)]){
+ var newMethod = self["make"+can.capitalize(name)](self[name]);
+ can.Construct._overwrite(self, base, name,function(){
+ this._super;
+ self._reqs++;
+ return newMethod.apply(self, arguments).then(clean, clean);
+ })
+ }
});
- var clean = can.proxy(this._clean, self);
- can.each({findAll : "models", findOne: "model"}, function(method, name){
-
- var old = self[name];
- can.Construct._overwrite(self, base, name, function(params, success, error){
- // this._super to trick it to load super
- this._super;
- // Increment requests.
- self._reqs++;
- // Make the request.
- return pipe( old.call( this, params ),
- this,
- method ).then(success,error).then(clean, clean);
- });
- })
- // Convert `findAll` and `findOne`.
- var oldFindAll
+
+
if(self.fullName == "can.Model"){
self.fullName = self._shortName = "Model"+(++modelNum);
}
@@ -673,9 +668,9 @@ steal('can/observe', function() {
return;
}
- if ( instancesRawData instanceof this.List ) {
- return instancesRawData;
- }
+ if ( instancesRawData instanceof this.List ) {
+ return instancesRawData;
+ }
// Get the list type.
var self = this,
@@ -1022,6 +1017,19 @@ steal('can/observe', function() {
}
});
+
+
+
+ can.each({makeFindAll : "models", makeFindOne: "model"}, function(method, name){
+ can.Model[name] = function(oldFind){
+ return function(params, success, error){
+ return pipe( oldFind.call( this, params ),
+ this,
+ method ).then(success,error)
+ }
+ };
+ });
+
can.each([
/**
* @function created
View
@@ -641,4 +641,80 @@ test("templated destroy", function(){
new MyModel({id: 5}).destroy(function(){
start();
})
+});
+
+test("overwrite makeFindAll", function(){
+
+ var store = {};
+
+ var LocalModel = can.Model({
+ makeFindOne : function(findOne){
+ return function(params, success, error){
+ var def = new can.Deferred(),
+ data = store[params.id];
+ def.then(success, error)
+ // make the ajax request right away
+ var findOneDeferred = findOne(params);
+
+ if(data){
+ var instance= this.model(data);
+ findOneDeferred.then(function(data){
+ instance.updated(data)
+ }, function(){
+ can.trigger(instance,"error", data)
+ });
+ def.resolve(instance)
+ } else {
+ findOneDeferred.then(can.proxy(function(data){
+ var instance= this.model(data);
+ store[instance[this.id]] = data;
+ def.resolve(instance)
+ }, this), function(data){
+ def.reject(data)
+ })
+ }
+ return def;
+ }
+ }
+ },{
+ updated : function(attrs){
+ can.Model.prototype.updated.apply(this, arguments);
+ store[this[this.constructor.id]] = this.serialize();
+ }
+ });
+
+
+ can.fixture("/food/{id}", function(settings){
+
+ return count == 0 ? {
+ id: settings.data.id,
+ name : "hot dog"
+ } : {
+ id: settings.data.id,
+ name : "ice water"
+ }
+ })
+ var Food = LocalModel({
+ findOne : "/food/{id}"
+ },{});
+ stop();
+ var count = 0;
+ Food.findOne({id: 1}, function(food){
+ count = 1;
+ ok(true, "empty findOne called back")
+ food.bind("name", function(){
+ ok(true, "name changed");
+ equal(count, 2, "after last find one")
+ equals(this.name, "ice water");
+ start();
+ })
+
+ Food.findOne({id: 1}, function(food2){
+ count = 2;
+ ok(food2 === food, "same instances")
+ equals(food2.name, "hot dog")
+ })
+ })
})
+
+

0 comments on commit 235097a

Please sign in to comment.