Permalink
Browse files

Ember.Resource: added copy() as well as Ember.Copyable mixin

  • Loading branch information...
1 parent 8d45099 commit ac525e5e4dd9df12db8425ee0c7f53c3d9afde58 @dgeb dgeb committed Apr 3, 2012
Showing with 31 additions and 5 deletions.
  1. +2 −1 CHANGELOG
  2. +18 −4 src/ember-rest.js
  3. +11 −0 tests/tests/ember-resource.js
View
3 CHANGELOG
@@ -1,6 +1,7 @@
*Ember-REST 0.1.1 (April 2, 2012)*
-* ResourceController._resourceUrl() can now extract a resourceUrl from a Resource that hasn't been instantiated
+* Ember.ResourceController._resourceUrl() can now extract a resourceUrl from an Ember.Resource that hasn't been instantiated
+* Ember.Resource: added copy() as well as Ember.Copyable mixin
*Ember-REST 0.1.0 (April 2, 2012)*
View
22 src/ember-rest.js
@@ -65,7 +65,7 @@ if (Ember.ResourceAdapter === undefined) {
* `deserializeProperty(prop, value)`
* `validate()`
*/
-Ember.Resource = Ember.Object.extend({
+Ember.Resource = Ember.Object.extend(Ember.ResourceAdapter, Ember.Copyable, {
resourceIdField: 'id',
resourceUrl: Ember.required(),
@@ -88,6 +88,20 @@ Ember.Resource = Ember.Object.extend({
},
/**
+ Create a copy of this resource
+
+ Needed to implement Ember.Copyable
+
+ REQUIRED: `resourceProperties`
+ */
+ copy: function(deep) {
+ var c = this.constructor.create();
+ c.duplicateProperties(this);
+ c.set(this.resourceIdField, this.get(this.resourceIdField));
+ return c;
+ },
+
+ /**
Generate this resource's JSON representation
Override this or `serializeProperty` to provide custom serialization
@@ -224,7 +238,7 @@ Ember.Resource = Ember.Object.extend({
_resourceId: function() {
return this.get(this.resourceIdField);
}
-}, Ember.ResourceAdapter);
+});
/**
A controller for RESTful resources
@@ -236,7 +250,7 @@ Ember.Resource = Ember.Object.extend({
* `resourceUrl` -- (optional) the base url of the resource (e.g. '/contacts/active');
will default to the `resourceUrl` for `resourceType`
*/
-Ember.ResourceController = Ember.ArrayController.extend({
+Ember.ResourceController = Ember.ArrayController.extend(Ember.ResourceAdapter, {
resourceType: Ember.required(),
/**
@@ -312,4 +326,4 @@ Ember.ResourceController = Ember.ArrayController.extend({
}
return this.resourceUrl;
}
-}, Ember.ResourceAdapter);
+});
View
11 tests/tests/ember-resource.js
@@ -10,6 +10,17 @@ module("Ember.Resource", {
}
});
+test("should be copyable", function() {
+ var props = {id: 1, first_name: "Joe", last_name: "Blow"};
+
+ var contact = Contact.create(props);
+ var copy = contact.copy();
+
+ equal(copy.get("id"), contact.get("id"), "id matches");
+ equal(copy.get("first_name"), contact.get("first_name"), "first_name matches");
+ equal(copy.get("last_name"), contact.get("last_name"), "last_name matches");
+});
+
test("should serialize its properties to JSON", function() {
var props = {id: 1, first_name: "Joe", last_name: "Blow"};

0 comments on commit ac525e5

Please sign in to comment.