Skip to content

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.
...
  • 3 commits
  • 5 files changed
  • 0 commit comments
  • 1 contributor
View
23 README.md
@@ -50,6 +50,19 @@ The REST adapter will send bulk commits to your server by default. If your
REST API does not support bulk operations, you can turn them off by specifying the
`bulkCommit` option (as illustrated above.)
+As well as `bulkCommit` the REST adapater also supports the following options:
+
+```javascript
+var adaptor = DS.RESTAdapter.create({
+ bulkCommit: false,
+ baseUrl: '/data/',
+ plurals: { person: 'people' }
+});
+```
+
+The REST adapter will default `baseUrl` to `'/'` and plurals will be empty and
+assume `'s'` should be appended to the singular modal name.
+
The RESTful adapter is still in progress. For Rails applications, we plan to make
it work seamlessly with the `active_model_serializers` gem's conventions. In
the meantime, see the section on rolling your own adapter.
@@ -84,6 +97,16 @@ App.Person = DS.Model.extend({
});
```
+For attributes that contain an array of primitives you can use `DS.attrArray`:
+
+```javascript
+App.Person = DS.Model.extend({
+ firstName: DS.attr('string'),
+ middleNames: DS.attrArray('string'),
+ lastName: DS.attr('string')
+});
+```
+
Valid attribute types are `string`, `integer`, `boolean`, and `date`. You
can also register custom attribute types. For example, here's a `boolString`
attribute type that converts booleans into the string `"Y"` or `"N"`:
View
24 packages/ember-data/lib/adapters/rest_adapter.js
@@ -7,7 +7,7 @@ DS.RESTAdapter = DS.Adapter.extend({
var data = {};
data[root] = get(model, 'data');
- this.ajax("/" + this.pluralize(root), "POST", {
+ this.ajax(this.baseUrl + this.pluralize(root), "POST", {
data: data,
success: function(json) {
store.didCreateRecord(model, json[root]);
@@ -28,7 +28,7 @@ DS.RESTAdapter = DS.Adapter.extend({
return get(model, 'data');
});
- this.ajax("/" + this.pluralize(root), "POST", {
+ this.ajax(this.baseUrl + this.pluralize(root), "POST", {
data: data,
success: function(json) {
store.didCreateRecords(type, models, json[plural]);
@@ -44,7 +44,7 @@ DS.RESTAdapter = DS.Adapter.extend({
var data = {};
data[root] = get(model, 'data');
- var url = ["", this.pluralize(root), id].join("/");
+ var url = this.baseUrl + [this.pluralize(root), id].join("/");
this.ajax(url, "PUT", {
data: data,
@@ -67,7 +67,7 @@ DS.RESTAdapter = DS.Adapter.extend({
return get(model, 'data');
});
- this.ajax("/" + this.pluralize(root), "POST", {
+ this.ajax(this.baseUrl + this.pluralize(root), "POST", {
data: data,
success: function(json) {
store.didUpdateRecords(models, json[plural]);
@@ -80,7 +80,7 @@ DS.RESTAdapter = DS.Adapter.extend({
id = get(model, primaryKey);
var root = this.rootForType(type);
- var url = ["", this.pluralize(root), id].join("/");
+ var url = this.baseUrl + [this.pluralize(root), id].join("/");
this.ajax(url, "DELETE", {
success: function(json) {
@@ -103,7 +103,7 @@ DS.RESTAdapter = DS.Adapter.extend({
return get(model, primaryKey);
});
- this.ajax("/" + this.pluralize(root) + "/delete", "POST", {
+ this.ajax(this.baseUrl + this.pluralize(root) + "/delete", "POST", {
data: data,
success: function(json) {
store.didDeleteRecords(models);
@@ -114,7 +114,7 @@ DS.RESTAdapter = DS.Adapter.extend({
find: function(store, type, id) {
var root = this.rootForType(type);
- var url = ["", this.pluralize(root), id].join("/");
+ var url = this.baseUrl + [this.pluralize(root), id].join("/");
this.ajax(url, "GET", {
success: function(json) {
@@ -126,19 +126,19 @@ DS.RESTAdapter = DS.Adapter.extend({
findMany: function(store, type, ids) {
var root = this.rootForType(type), plural = this.pluralize(root);
- this.ajax("/" + plural, "GET", {
+ this.ajax(this.baseUrl + plural, "GET", {
data: { ids: ids },
success: function(json) {
store.loadMany(type, ids, json[plural]);
}
});
- var url = "/" + plural;
+ var url = this.baseUrl + plural;
},
findAll: function(store, type) {
var root = this.rootForType(type), plural = this.pluralize(root);
- this.ajax("/" + plural, "GET", {
+ this.ajax(this.baseUrl + plural, "GET", {
success: function(json) {
store.loadMany(type, json[plural]);
}
@@ -148,7 +148,7 @@ DS.RESTAdapter = DS.Adapter.extend({
findQuery: function(store, type, query, modelArray) {
var root = this.rootForType(type), plural = this.pluralize(root);
- this.ajax("/" + plural, "GET", {
+ this.ajax(this.baseUrl + plural, "GET", {
data: query,
success: function(json) {
modelArray.load(json[plural]);
@@ -158,6 +158,8 @@ DS.RESTAdapter = DS.Adapter.extend({
// HELPERS
+ baseUrl: '/',
+
plurals: {},
// define a plurals hash in your subclass to define
View
31 packages/ember-data/lib/system/model.js
@@ -394,6 +394,37 @@ DS.attr = function(type, options) {
}).property('data');
};
+DS.attrArray = function(type, options) {
+ var transform = DS.attr.transforms[type];
+ var transformFrom = transform.from;
+ var transformTo = transform.to;
+
+ return Ember.computed(function(key, value) {
+ var data = get(this, 'data'),
+ i, values = [];
+
+ key = (options && options.key) ? options.key : key;
+
+ if (value === undefined) {
+ if (data && jQuery.isArray(data[key])) {
+ for (i = 0; i < data[key].length; i++) {
+ values.push(transformFrom(data[key][i]));
+ }
+ }
+ return values;
+ } else {
+ ember_assert("You cannot set a model attribute before its data is loaded.", !!data);
+ if (jQuery.isArray(value)) {
+ for (i = 0; i < value.length; i++) {
+ values.push(transformTo(value[i]));
+ }
+ }
+ this.setProperty(key, values);
+ return values;
+ }
+ }).property('data');
+};
+
var embeddedFindRecord = function(store, type, data, key, one) {
var association = data ? get(data, key) : one ? null : [];
if (one) {
View
29 packages/ember-data/tests/model_test.js
@@ -112,6 +112,35 @@ test("a DS.Model can describe Date attributes", function() {
convertsWhenSet('date', date, dateString);
});
+test("a DS.Model can describe array attributes", function() {
+ var names = ['john', 'james', 'jack'];
+ var numbers = [5, 9, 456, 68];
+ var dates = [new Date(2011,11,11), new Date(2012,12,12)];
+ var bools = [true, false, false, false];
+
+ var model = DS.Model.create({
+ names: DS.attrArray('string'),
+ numbers: DS.attrArray('integer'),
+ dates: DS.attrArray('date'),
+ bools: DS.attrArray('boolean')
+ });
+
+ model.loadingData();
+ model.setData({});
+
+ model.set('names', names);
+ deepEqual(names, get(model, 'names'), "setting an array of strings returns the same array");
+
+ model.set('numbers', numbers);
+ deepEqual(numbers, get(model, 'numbers'), "setting an array of integers returns the same array");
+
+ model.set('dates', dates);
+ deepEqual(dates, get(model, 'dates'), "setting an array of dates returns the same array");
+
+ model.set('bools', bools);
+ deepEqual(bools, get(model, 'bools'), "setting an array of booleans returns the same array");
+});
+
test("it can specify which key to use when looking up properties on the hash", function() {
var model = DS.Model.create({
name: DS.attr('string', { key: 'full_name' })
View
16 packages/ember-data/tests/rest_adapter_test.js
@@ -361,3 +361,19 @@ test("deleting several people (with bulkCommit) makes a POST to /people/delete_m
expectStates('deleted');
expectStates('dirty', false);
});
+
+test("finding a person with baseUrl set to '/data/' makes a GET to /data/people/:id", function() {
+ set(adapter, 'baseUrl', '/data/');
+ person = store.find(Person, 1);
+
+ expectState('loaded', false);
+ expectUrl("/data/people/1", "the plural of the model name with the ID requested");
+ expectType("GET");
+
+ ajaxHash.success({ person: { id: 1, name: "Yehuda Katz" } });
+
+ expectState('loaded');
+ expectState('dirty', false);
+
+ equal(person, store.find(Person, 1), "the record is now in the store, and can be looked up by ID without another Ajax request");
+});

No commit comments for this range

Something went wrong with that request. Please try again.