Question

This may seem like a simple question, but how can I use an external URL with Ember-model? All of the examples just assume the same domain. I would like to use e.g. apiary or firebase.

https://github.com/ebryn/ember-model

Was it helpful?

Solution

You can just set the full url as the property that gets set for the model instead of a relative URL. Like this:

App.User = Ember.Model.extend({
  id: attr(),
  name: attr(),
  comments: hasMany("App.Comment", {key: 'comment_ids'})
});

App.User.url = "http://example.com/users";

Update:

If you don't want to specify the hostname in multiple places, the simplest thing to do would probably be to assign the hostname to a variable and then reference the variable when you're declaring the URL. If you really want to get into the ember model internals, though, you could also override the buildURL method in a custom adapter, like this:

App.CustomAdapter = Ember.RESTAdapter.extend({
  buildURL: function(klass, id) {
    var urlRoot = "http://example.com/" + Ember.get(klass, 'url');
    if (!urlRoot) { throw new Error('Ember.RESTAdapter requires a `url` property to be specified'); }

    if (!Ember.isEmpty(id)) {
      return urlRoot + "/" + id + ".json";
    } else {
      return urlRoot + ".json";
    }
  }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top