gpt4 book ai didi

backbone.js - Backbone 将模型传递给路由器

转载 作者:行者123 更新时间:2023-12-04 06:04:06 29 4
gpt4 key购买 nike

我正在使用 require js 和 Backbone 为 android 开发应用程序。我必须通过 touchend 事件将从集合中获取的模型传递给路由器。我该怎么做?

define(["jquery", "underscore","backbone","handlebars", "views/CinemaView", "models/CineDati",  "text!templates/listacinema.html"],

function($,_,Backbone,Handlebars,CinemaView, CineDati, template){
var ListaCinemaView = Backbone.View.extend({
template: Handlebars.compile(template),
events: {
"touchend" : "Details"
},
initialize : function (){
var self = this;
$.ajax({
url: 'http://www.cineworld.com/api/quickbook/cinemas',
type: 'GET',
data: {key: 'BwKR7b2D'},
dataType: 'jsonp', // Setting this data type will add the callback parameter for you
success: function (response, status) {
// Check for errors from the server
if (response.errors) {
$.each(response.errors, function() {
alert('An error occurred. Please Try Again');
});
} else {

$.each(response.cinemas, function() {
var cinema = new CineDati();
cinema.set({ id : this.id, name : this.name , cinema_url : this.cinema_url, address: this.address, postcode : this.postcode , telephone : this.telephone });
self.model.add([cinema]);

});
self.render();
}}
});


},

events : {
"#touchend" : Dettagli
},

render : function(){
$(this.el).empty();

$(this.el).html(template).append(
_.each(this.model.models, function (cinema) {
$("#lista").append(new CinemaView({
model: cinema
}).render().el); }, this));

return this;
},

Dettagli : function(){

Backbone.history.navigate( this.model , {trigger: "true"});
}


});
return ListaCinemaView;

});

最佳答案

您需要按如下方式覆盖 Backbone 的导航功能:

var Router = Backbone.Router.extend({
routeParams: {},
routes: {
"home": "onHomeRoute"
},
/*
*Override navigate function
*@param {String} route The route hash
*@param {PlainObject} options The Options for navigate functions.
* You can send a extra property "params" to pass your parameter as following:
* {
* params: 'data'
* }
**/
navigate: function(route, options) {
var routeOption = {
trigger: true
},
params = (options && options.params) ? options.params : null;
$.extend(routeOption, options);
delete routeOption.params;

//set the params for the route
this.param(route, params);
Backbone.Router.prototype.navigate(route, routeOption);
},

/*
*Get or set parameters for a route fragment
*@param {String} fragment Exact route hash. for example:
* If you have route for 'profile/:id', then to get set param
* you need to send the fragment 'profile/1' or 'profile/2'
*@param {Any Type} params The parameter you to set for the route
*@return param value for that parameter.
**/
param: function(fragment, params) {
var matchedRoute;
_.any(Backbone.history.handlers, function(handler) {
if (handler.route.test(fragment)) {
matchedRoute = handler.route;
}
});
if (params !== undefined) {
this.routeParams[fragment] = params;
}

return this.routeParams[fragment];
},

/*
* Called when hash changes to home route
**/
onHomeRoute: function() {
console.log("param =", this.param("home"));
}

})


在这里,我编写了一个自定义函数“param”,用于获取/设置要发送的参数。

现在要发送自定义参数,您可以像下面这样发送它:

var router = new Router();
Backbone.history.start({});
router.navigate("home", {
params: 'Your data'
});


要在回调函数内检索获取数据中的数据,您可以编写如下代码:

this.param("home"); //this will return the parameter you set while calling navigate function or else will return null

关于backbone.js - Backbone 将模型传递给路由器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18430086/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com