Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
View
5 CHANGELOG
@@ -1,3 +1,8 @@
+*Ember-REST 0.1.1 (April 2, 2012)*
+
+* 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)*
* Added versioning to Ember-REST
View
47 src/ember-rest.js
@@ -1,5 +1,5 @@
/**
- Ember-REST.js 0.1.0
+ Ember-REST.js 0.1.1
A simple library for RESTful resources in Ember.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(),
/**
@@ -292,9 +306,24 @@ Ember.ResourceController = Ember.ArrayController.extend({
the `resourceUrl` specified for `resourceType`.
*/
_resourceUrl: function() {
- if (this.resourceUrl === undefined)
- return this.get('resourceType').prototype.resourceUrl;
- else
- return this.resourceUrl;
+ if (this.resourceUrl === undefined) {
+ // If `resourceUrl` is not defined for this controller, there are a couple
+ // ways to retrieve it from the resource. If a resource has been instantiated,
+ // then it can be retrieved from the resource's prototype. Otherwise, we need
+ // to loop through the mixins for the prototype to get the resourceUrl.
+ var rt = this.get('resourceType');
+ if (rt.prototype.resourceUrl === undefined) {
+ for (var i = rt.PrototypeMixin.mixins.length - 1; i >= 0; i--) {
+ var m = rt.PrototypeMixin.mixins[i];
+ if (m.properties !== undefined && m.properties.resourceUrl !== undefined) {
+ return m.properties.resourceUrl;
+ }
+ }
+ }
+ else {
+ return rt.prototype.resourceUrl;
+ }
+ }
+ return this.resourceUrl;
}
-}, Ember.ResourceAdapter);
+});
View
5 tests/tests/ember-resource-controller.js
@@ -20,6 +20,11 @@ test("should use the resourceUrl from its corresponding resource", function() {
equal(contactsController._resourceUrl(), "/contacts");
});
+test("should use the resourceUrl from a corresponding resource which has not yet been instantiated", function() {
+ contactsController.set("resourceType", Ember.Resource.extend({resourceUrl: '/people'}));
+ equal(contactsController._resourceUrl(), "/people");
+});
+
test("should be able to override resourceUrl", function() {
contactsController.set("resourceUrl", "/contacts/active");
equal(contactsController._resourceUrl(), "/contacts/active");
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"};

No commit comments for this range

Something went wrong with that request. Please try again.