开发者

BackboneJS: Map RESTful URL to old-school HTTP Query URL

开发者 https://www.devze.com 2023-04-06 09:03 出处:网络
I\'ve just picked up BackboneJS for a work experiment. We are implementing a whole slew of web-apps on our platform so using a JS framework would simplify a lot of stuff. However, the way to query the

I've just picked up BackboneJS for a work experiment. We are implementing a whole slew of web-apps on our platform so using a JS framework would simplify a lot of stuff. However, the way to query the server is to use regular HTTP 开发者_如何学Cquery strings appended to back of a script name; for example, getSomething?id=0&name=2. Basically I am wondering if it is possible in backbone (or, if someone can recommend another popular framework, that one) to map something like /getSomething/:id/:name to the HTTP query string basically making it /service/rest/getSomething?id=:id&name=:name.

Any help is appreciated. Thanks.


Backbone doesn't actually require you to use REST and the like with its models. If you decide to skip that, they still offer the ability for you to update models using the .set() to assign a new set of values you retrieve via another method. I've actually got several models where I may hit .fetch() on the model, but it will perform one or several calls to the server to get data and set it back onto the model. Our back end services just don't happen to be a nice fit to the sync setup backbone expects for its built in saving and fetching of models.

Small clarification: I should say that what I've done here is override the "fetch" function on my models with an alternate implementation that uses jQuery $.ajax() calls.

I'm not sure about the whole question you asked originally though with respect to mapping URLs. The URLs that the Backbone router takes are pulled apart and you get the individual pieces. I wouldn't anticipate the router ever making a direct call to the back end though, I would expect that the router would pass those through to a call on the model, which would then interface with the service to get data. Thus only the model layer sees the backend details of the services and any data mapping necessary, the router and the views stay clean of those kind of details.

0

精彩评论

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

关注公众号