开发者

Backbone.Js Endpoints / Rails Integration and URL Params

开发者 https://www.devze.com 2023-02-20 05:28 出处:网络
In my Rails application I have videos. Videos are linked to Yo开发者_Go百科utube so sometimes they become unavailable.

In my Rails application I have videos. Videos are linked to Yo开发者_Go百科utube so sometimes they become unavailable.

When I query my videos endpoint in Rails I will sometimes pass in a "request_type=" url attribute to indicate I want to get videos that have broken links.

To do this, in backbone.js I've simply overwritten the url method and used jQuerys params command to add the url parameters. The problem I have now is with updating existing items. It's trying to use a URL like the following:

PUT /medias?request_type=broken_medias/2

Clearly the /2 needs to appear after the /medias portion the the url. It doesn't matter if the request_type params stays on the URL although I would prefer it incase I need to handle specific update behavior for broken medias.

Any thoughts on how best to handle this? Unique routes for broken medias? Check if I'm doing an PUT request in the collection? Override the url method just before doing an update request?


In order to fully control the backend requests with Backbone you need to override your Collections or Models sync method which actually builds and makes the ajax request. See an example here: Backbone.js updating of models in a collection

UPDATE: I gave it some more thought upon Juliens comment and remembered that url attribute in Collections or models could also be a function where you can build your url.

var myModel = Backbone.Model.extend({
   url: function(){
      // build my_url
      // based on my specific rules
      return my_url;
   }
})
0

精彩评论

暂无评论...
验证码 换一张
取 消