Giter Site home page Giter Site logo

onceknown / backbone-model-url-templating Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dmitriykharchenko/backbone-model-url-templating

0.0 3.0 0.0 63 KB

Plugin to Backbone.js to allow for model URL templating.

License: Other

JavaScript 100.00%

backbone-model-url-templating's Introduction

backbone-model-url-templating

Plugin to Backbone.js to allow for model URL templating.

Model URL templating are a handy way to create models that have service urls that change depending on the model's data. This is useful for services that have complex URL patterns.

Usage

URL templating allows for the URL of sync calls to be dynamically generated according to a template. By default the URL template is read from the url property of the model. A different URL for each sync method can be specified in the urls property as key/value pairs.

Basic syntax

var Model = Backbone.ServiceModel.extend({
	url: 'path/to/some/api/{id}.json'
});

var model = new Model({ id: 12345 });
model.fetch(); // will make service call to 'path/to/some/api/12345.json'

Interpolation

The URL template uses the { property } syntax. All values are automatically URL encoded and can be assumed safe. Any properties in the Model attributes or defined as static properties are available in the template.

'path/to/some/service/{id}.json' => 'path/to/some/service/12345.json'

The URL template may also do more complex interpolation since any valid javascript code may be executed within braces.

'path/to/{ id ? id : "123" }' => '/path/to/123' or '/path/to/someId'
'path/to/{ name.split(" ").join(".").toLowerCase() }' => '/path/to/john.smith'

Method level templating

The Backbone.Model calls sync with the different methods depending on the state of the model. This is described in Backbone's Sync documentation. The urls property can be used to map the method call to a specific URL. If no method mapping is defined in the URLs property, it will default to the url property instead.

urls: {
	'create': '/path/to/create/url',
	'read': 'path/to/read/url',
	'update': 'path/to/update/url',
	'delete': 'path/to/delete/url'
}

URL function callbacks

Along with the string templating, URLs may also be in the form of a callback function. The function is called with the context of the current model and expects to return a string as the result. Since the result is processed through the interpolation engine, the result may also contain templating syntax.

url: function (){ return this.get('name'); },

urls: {
	'create': function (){ return 'path/to/{id}'; }
}

Support

To suggest a feature, report a bug, or general discussion: http://github.com/huffingtonpost/backbone-model-url-templating/issues/

backbone-model-url-templating's People

Contributors

denen99 avatar dmitriykharchenko avatar jsiragusa avatar mikeedwards avatar mousius avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.