Last active
January 18, 2016 18:49
-
-
Save dfreeman/98db5b99cb47b52a5560 to your computer and use it in GitHub Desktop.
double-encoded-id
This file contains hidden or 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
import Ember from 'ember'; | |
import DS from 'ember-data'; | |
import URLTemplates from '../mixins/url-templates'; | |
export default Ember.Controller.extend({ | |
id: 'abc xyz', | |
badUrl: Ember.computed('id', function() { | |
return this.defaultAdapter.buildURL('thing', this.get('id')); | |
}), | |
goodUrl: Ember.computed('id', function() { | |
return this.customAdapter.buildURL('thing', this.get('id')); | |
}), | |
// Adapter with the default ID segment | |
defaultAdapter: DS.RESTAdapter.extend(URLTemplates, { | |
urlTemplate: '{pathForType}{/id}' | |
}).create(), | |
// Adapter with the ID segment overridden to just return raw | |
customAdapter: DS.RESTAdapter.extend(URLTemplates, { | |
urlTemplate: '{pathForType}{/id}', | |
urlSegments: { | |
id: (type, id) => id | |
} | |
}).create() | |
}); |
This file contains hidden or 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
import Ember from 'ember'; | |
/* global UriTemplate */ | |
var isArray = Ember.isArray; | |
var sanitize = encodeURIComponent; | |
var isObject = function(object) { return typeof object === 'object'; }; | |
export default Ember.Mixin.create({ | |
mergedProperties: ['urlSegments'], | |
buildURL: function(type, id, snapshot, requestType, query) { | |
var template = this.getTemplate(requestType); | |
if (!template) { | |
return this._super(...arguments); | |
} | |
template = this.compileTemplate(template); | |
var templateResolver = this.templateResolverFor(type); | |
var adapter = this; | |
return template.fill(function(name) { | |
var result = templateResolver.get(name); | |
if (Ember.typeOf(result) === 'function') { | |
return result.call(adapter, type, id, snapshot, query); | |
} else { | |
return result; | |
} | |
}); | |
}, | |
getTemplate: function(requestType) { | |
return this.get(requestType + 'UrlTemplate') || this.get('urlTemplate'); | |
}, | |
compileTemplate: function(template) { | |
return new UriTemplate(template); | |
}, | |
// TODO: Add ability to customize templateResolver | |
// TODO: Add reference to the adapter | |
templateResolverFor: function(/* type */) { | |
return Ember.Object.create(this.get('urlSegments')); | |
}, | |
urlSegments: { | |
host: function () { return this.get('host'); }, | |
namespace: function() { return this.get('namespace'); }, | |
pathForType: function(type) { return this.pathForType(type); }, | |
id: function(type, id) { | |
if (id && !isArray(id) && !isObject(id)) { return sanitize(id); } | |
}, | |
query: function(type, id, snapshot, query) { | |
return query; | |
}, | |
// TODO: Support automatic relationship ids through snapshots api. | |
unknownProperty: function(key) { | |
return function(type, id, snapshot, query) { | |
if (query && query[key]) { return query[key]; } | |
if (snapshot) { return snapshot[key]; } | |
}; | |
} | |
} | |
}); |
This file contains hidden or 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
{ | |
"version": "0.5.0", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.2.0/ember.debug.js", | |
"ember-data": "2.2.0", | |
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.2.0/ember-template-compiler.js", | |
"uri-templates": "https://rawgit.com/geraintluff/uri-templates/master/uri-templates.js" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment