Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js 将模型传递给路由器的主干网_Backbone.js - Fatal编程技术网

Backbone.js 将模型传递给路由器的主干网

Backbone.js 将模型传递给路由器的主干网,backbone.js,Backbone.js,我正在使用requirejs和Backbone为android开发一个应用程序。我必须通过touchend事件将采集的模型传递给路由器。我怎么做 define(["jquery", "underscore","backbone","handlebars", "views/CinemaView", "models/CineDati", "text!templates/listacinema.html"], function($,_,Backbone,Handlebars,CinemaView,

我正在使用requirejs和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;

});    
Router.navigate()
不传递任何数据。它设置url片段并使用几个选项。请参见此处的文档:

我的建议是:

  • 使用
    Router.navigate()
    更改URL
  • 使用
    主干.Events
    聚合器触发(或发布)事件和数据
假设你有一个电影列表,还有一个查看按钮。“视图”按钮发布它想要显示的模型,并更改URL片段

var vent = _.extend( {}, Backbone.Events ); // Create your app specific event aggregator

var ListaCinemaView = Backbone.View.extend({

    ...

    Dettagli : function(){
        vent.trigger('movie:show:request', this.model);

        Backbone.history.navigate( this.model.get('id') );
    }
}
在应用程序中的其他位置为
电影:查看:请求添加处理程序

vent.on('movie:show:request', showMovieDetails);

var showMovieDetails = function(model) { ... }

最后,看看。它使用发布/订阅模式来处理应用程序各部分之间的通信。这是一个非常好的框架,因为您基本上选择了要使用的部分。它有很好的文档记录和支持。此外,它的创建者在Stackoverflow上非常活跃,因此您可以快速获得帮助。

您需要覆盖主干的导航功能,如下所示:

var-Router=Backbone.Router.extend({
路线图:{},
路线:{
“home”:“onHomeRoute”
},
/*
*覆盖导航功能
*@param{String}路由路由哈希
*@param{PlainObject}options导航函数的选项。
*您可以发送额外的属性“params”来传递参数,如下所示:
*              {
*参数:“数据”
*              }
**/
导航:功能(路线、选项){
变量路由选项={
触发器:正确
},
params=(options&&options.params)?options.params:null;
$.extend(routeOptions,选项);
删除routeOptions.params;
//设置路由的参数
this.param(路线,参数);
主干网。路由器。原型。导航(路由,路由选项);
},
/*
*获取或设置路由片段的参数
*@param{String}片段精确路由哈希。例如:
*如果您有“profile/:id”的路由,则要获取set param
*您需要发送片段“profile/1”或“profile/2”
*@param{Any Type}param是要为路由设置的参数
*@返回该参数的参数值。
**/
参数:函数(片段,参数){
var匹配路由;
_.any(主干.history.handlers,函数(handler){
if(handler.route.test(片段)){
matchedRoute=handler.route;
}
});
如果(参数!==未定义){
this.routeParams[fragment]=params;
}
返回此.routeParams[片段];
},
/*
*当散列更改为主路由时调用
**/
onHomeRoute:函数(){
console.log(“param=”,this.param(“home”);
}

})
非常感谢威尔,你帮了我很大的忙!我已经签出了marionette.js,我想我会在未来的应用程序中使用它!:)很高兴我帮忙,奥古斯托。祝你的项目好运。此解决方案不考虑某人在打开页面后刷新页面的情况。您将丢失对模型的引用。