Skip to content

Instantly share code, notes, and snippets.

@polidog
Created March 22, 2014 15:08
Show Gist options
  • Save polidog/9708665 to your computer and use it in GitHub Desktop.
Save polidog/9708665 to your computer and use it in GitHub Desktop.
TwigをつかってBackboneのModelとかCollectionとかでリクエスト先のURLを指定する
<script>
var HogeModel = Backbone.Model.extend({
methodToURL: {
'read' : '{{ path("get_hoge") }}',
'create': '{{ path("post_hoge") }}',
'update': '{{ path("put_hoge", {"id": "id"}) }}',
'delete': '{{ path("delete_hoge", {"id": "id"}) }}'
},
defaults: function() {
return {};
},
sync: function(method, model, options) {
options = options || {};
options.url = (this.methodToURL[method.toLowerCase()] || options.url).replace('id', model.get('id'));
Backbone.sync(method, model, options);
}
});
var HogeCollection = Backbone.Collection.extend({
model: ConflictSite,
url: '{{ path("get_hoges") }}',
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment