【问题标题】:Backbone.js - Pass variable from route to view/collection/modelBackbone.js - 将变量从路由传递到视图/集合/模型
【发布时间】:2014-08-14 20:46:34
【问题描述】:

所以我正在构建一个移动网站,并且我有一个名为“api”的目录,其中包含来自远程 API 的各种 php 文件与 JSON 格式的数据相呼应。我这样做是为了避免跨域问题。

但是其中一个 php 文件需要一个 GET 参数(即 id),以便我可以根据它的 id 回显特定对象的 JSON 数据。

我的收藏需要这样做(假设这可行):

define([
    'backbone',
    'models/tournaments/single'
], function(Backbone, singleModel) {
    var TournamentCollection = Backbone.Collection.extend({
        model: singleModel,
        url: '/api/tournament.php?id=' + id,
        parse: function(response) {
            return response;
        }
    });
    return TournamentCollection;
});

我的路由器中有这个,但是如何将“id”值传递给视图或集合:

define([
    'jquery',
    'underscore',
    'backbone',
    'views/home',
    'views/tournament'
], function($, _, Backbone, HomeView, TournamentView) {

    var AppRouter = Backbone.Router.extend({
        routes: {
            '': 'home',
            'tournament/:id': 'tournament'
        }
    });

    var initialize = function() {
        var app_router = new AppRouter;

        app_router.on('route:home', function() {
            var homeView = new HomeView();
        });

        app_router.on('route:tournament', function(id) {
            var tournamentView = new TournamentView({id: id});
        });

        Backbone.history.start();
    };

    return {
        initialize: initialize
    };
});

【问题讨论】:

  • 不是 100% 确定您在问什么...您是否希望集合从服务器获取模型,然后将其传递到视图中,或者您是否希望专门设置 id 属性发送?有点困惑,因为您已经将它传递给 TournamentView
  • 如何在视图中读取“id”?我需要将“id”发送到集合。

标签: javascript php jquery json backbone.js


【解决方案1】:

几件事:

1) 您对集合的 url 属性的定义将不起作用,因为在定义 TournamentCollection 类时可能未定义 id。您可以使用函数而不是属性。 TournamentCollection 会变成这样:

define([
    'backbone',
    'models/tournaments/single'
], function(Backbone, singleModel) {
    var TournamentCollection = Backbone.Collection.extend({
        model: singleModel,
        initialize: function (options) {
          this.id = options.id;
        },
        url: function () {
          return '/api/tournament.php?id=' + this.id
        },
        parse: function(response) {
            return response;
        }
    });
    return TournamentCollection;
});

通过这种方式,您可以使用 id 初始化对象,然后,当获取 url 时,它将包含正确的 id。

2) 我可能会初始化并从路由器中获取集合。然后从视图的初始化开始,监听该获取以完成并最终重新渲染视图。像这样的:

define([
    'jquery',
    'underscore',
     'backbone',
     'views/home',
     'views/tournament'
 ], function($, _, Backbone, HomeView, TournamentView) {

     var AppRouter = Backbone.Router.extend({
         routes: {
             '': 'home',
             'tournament/:id': 'tournament'
         }
     });

     var initialize = function() {
         var app_router = new AppRouter;

         app_router.on('route:home', function() {
             var homeView = new HomeView();
         });

         app_router.on('route:tournament', function(id) {
             var tournaments = new TournamentCollection({ id: id });
             tournaments.fetch();
             var tournamentView = new TournamentView({ collection: tournaments });
         });

         Backbone.history.start();
     };

     return {
         initialize: initialize
     };
 });

 // Tournament View define stuff
 var TournamentView = Backbone.View.extend({
   initialize: function () {
     this.listenTo(this.collection, 'sync', this.render);
   },
   render: function () {
     //...
   }
 });
 return TournamentView

希望对您有所帮助。 :)

【讨论】:

  • 我做了所有这些但没有运气,调用 this.options.id 时 this.options 在集合中未定义。我在某处读到我可能需要在构造函数中将 id 作为属性传递,对此有什么想法吗?
  • var tournaments = new TournamentCollection({ id: id }); 在构造函数中传递 id。没有this.options.id。集合的初始化方法中有 options.id 可用。查看路线路线:锦标赛
  • 我通过在tournaments.fetch() 的成功方法中调用视图使其工作。我现在正在努力渲染新视图,请参见此处:stackoverflow.com/questions/25329922/…
猜你喜欢
  • 1970-01-01
  • 2015-06-04
  • 1970-01-01
  • 1970-01-01
  • 2016-01-23
  • 2015-06-05
  • 1970-01-01
  • 2014-08-23
  • 1970-01-01
相关资源
最近更新 更多