AmpersandJS / ampersand-model

Observable objects, for managing state in applications.
MIT License
84 stars 31 forks source link

Relative urlRoot #50

Open EvanCarroll opened 9 years ago

EvanCarroll commented 9 years ago

In the upcoming release of ampsersand-sync, xhr2.js will be used. There is already a bug and pull request tracking this https://github.com/AmpersandJS/ampersand-sync/issues/32 This gives us a great new feature we didn't previously have: we can test the interfaces outside of the browser, using Mocha. It also raises a question..

var model = BaseModel.extend({
    urlRoot: "/login"
});

What this does inside the browser is logical: it's a simple relative URL and all of it is handled behind the scenes. However, we'll have to hack ampersand-model.url() https://github.com/AmpersandJS/ampersand-model/blob/master/ampersand-model.js#L128 so we can reference some kind of global root path or something. If we don't have a reference to a global root path, or some way to emulate this functionality transparently everywhere ampersand-models are instantiated then we can we either

Add a computed lazy property that emulates something like baseURI, we'll use baseURL as an example here.

*. Default baseURL property with

  1. window.document.location.href (if this exists)
  2. if it doesn't exist default it to global.document.location.href
  3. throw fatal error.

This would permit us to always calculate the base for the current urlRoot.

. Add a convenience function, isRelativeURL to check whether or not urlRoot is relative. . Add a conditional here using the new isRelativeURL. If the root is relative, use a URL library to make the urlRoot relative to the baseURL.

Suggestions? Ideas? One consideration we may have is the ambiguity in names. Under the current scheme urlRoot is often not the root at all. It's often relative to the implicit url root of document.window.location.href. This doesn't fix this bad property name.