-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2812 from amiel/move-buildUrl-to-mixin
Move buildURL and related methods to a mixin
- Loading branch information
Showing
3 changed files
with
144 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
var get = Ember.get; | ||
|
||
/** | ||
WARNING: This interface is likely to change in order to accomodate https://github.com/emberjs/rfcs/pull/4 | ||
## Using BuildURLMixin | ||
To use url building, include the mixin when extending an adapter, and call `buildURL` where needed. | ||
The default behaviour is designed for RESTAdapter. | ||
### Example | ||
```javascript | ||
export default DS.Adapter.extend(BuildURLMixin, { | ||
find: function(store, type, id, record) { | ||
var url = this.buildURL(type.typeKey, id, record); | ||
return this.ajax(url, 'GET'); | ||
} | ||
}); | ||
``` | ||
### Attributes | ||
The `host` and `namespace` attributes will be used if defined, and are optional. | ||
@class BuildURLMixin | ||
@namespace DS | ||
*/ | ||
export default Ember.Mixin.create({ | ||
/** | ||
Builds a URL for a given type and optional ID. | ||
By default, it pluralizes the type's name (for example, 'post' | ||
becomes 'posts' and 'person' becomes 'people'). To override the | ||
pluralization see [pathForType](#method_pathForType). | ||
If an ID is specified, it adds the ID to the path generated | ||
for the type, separated by a `/`. | ||
@method buildURL | ||
@param {String} type | ||
@param {String} id | ||
@param {DS.Model} record | ||
@return {String} url | ||
*/ | ||
buildURL: function(type, id, record) { | ||
var url = []; | ||
var host = get(this, 'host'); | ||
var prefix = this.urlPrefix(); | ||
|
||
if (type) { url.push(this.pathForType(type)); } | ||
|
||
//We might get passed in an array of ids from findMany | ||
//in which case we don't want to modify the url, as the | ||
//ids will be passed in through a query param | ||
if (id && !Ember.isArray(id)) { url.push(encodeURIComponent(id)); } | ||
|
||
if (prefix) { url.unshift(prefix); } | ||
|
||
url = url.join('/'); | ||
if (!host && url) { url = '/' + url; } | ||
|
||
return url; | ||
}, | ||
|
||
/** | ||
@method urlPrefix | ||
@private | ||
@param {String} path | ||
@param {String} parentUrl | ||
@return {String} urlPrefix | ||
*/ | ||
urlPrefix: function(path, parentURL) { | ||
var host = get(this, 'host'); | ||
var namespace = get(this, 'namespace'); | ||
var url = []; | ||
|
||
if (path) { | ||
// Protocol relative url | ||
//jscs:disable disallowEmptyBlocks | ||
if (/^\/\//.test(path)) { | ||
// Do nothing, the full host is already included. This branch | ||
// avoids the absolute path logic and the relative path logic. | ||
|
||
// Absolute path | ||
} else if (path.charAt(0) === '/') { | ||
//jscs:enable disallowEmptyBlocks | ||
if (host) { | ||
path = path.slice(1); | ||
url.push(host); | ||
} | ||
// Relative path | ||
} else if (!/^http(s)?:\/\//.test(path)) { | ||
url.push(parentURL); | ||
} | ||
} else { | ||
if (host) { url.push(host); } | ||
if (namespace) { url.push(namespace); } | ||
} | ||
|
||
if (path) { | ||
url.push(path); | ||
} | ||
|
||
return url.join('/'); | ||
}, | ||
|
||
|
||
/** | ||
Determines the pathname for a given type. | ||
By default, it pluralizes the type's name (for example, | ||
'post' becomes 'posts' and 'person' becomes 'people'). | ||
### Pathname customization | ||
For example if you have an object LineItem with an | ||
endpoint of "/line_items/". | ||
```js | ||
App.ApplicationAdapter = DS.RESTAdapter.extend({ | ||
pathForType: function(type) { | ||
var decamelized = Ember.String.decamelize(type); | ||
return Ember.String.pluralize(decamelized); | ||
} | ||
}); | ||
``` | ||
@method pathForType | ||
@param {String} type | ||
@return {String} path | ||
**/ | ||
pathForType: function(type) { | ||
var camelized = Ember.String.camelize(type); | ||
return Ember.String.pluralize(camelized); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters