Backbone.js-将变量从路由传递到视图/集合/模型 - javascript

因此,我正在构建一个移动网站,并且有一个名为“ api”的目录,其中包含各种php文件,它们从远程API回显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
    };
});

参考方案

几件事情:

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

希望能有所帮助。 🙂

从php文件运行命令行程序 - javascript

我想从php文件执行任何命令。我有一个nodejs文件,当我给出命令执行某件事时。它工作正常。即abc.js参数在终端上可以正常工作并执行一些任务但是,我想从运行我的命令的php文件中执行相同的任务。我尝试了不起作用的exec('filename arguments')。如果我给exec(ls -l)给出输出。请让我知道您的建议。谢谢 参考方案 我认为您必须…

js:尝试将精细上传器挂接到flask应用程序中 - javascript

我正在尝试将上载器连接到Flask端点。端点看起来像:@app.route('/', methods=['GET', 'POST']) def a_function(): if request.method == 'POST': file = request.files[…

Angular JS ng-repeat无法通过PHP数组获取输出 - javascript

我是Angular JS的新手,我想在ng-repeat中显示一个php数组。这是我的html代码,<div ng-controller="clientList"> <ul> <li ng-repeat="client in clients"> <h2>{{client.…

异步调用nodejs和python - javascript

大家我想在节点脚本中异步调用python脚本。我为此使用“ python-shell”。我的问题是节点首先执行自己的代码,然后执行python脚本。我在运行时需要python脚本数据。这意味着:let {PythonShell} = require('python-shell'); let message; PythonShell.run…

如何在我的JS代码中使用flask.Response? - javascript

我已经尝试了一段时间,但未能设法从后端和前端获取返回值。这是我的代码:def add_New_Form(): resp = Response("something") #Note that the response value is a string return resp 我希望能够在前端使用它(我正在使用Vue)。我无法对resp进行…