简体   繁体   English

Backbone.js将url参数传递给集合

[英]Backbone.js passing url parameter to collection

I am using a test .json file and can console.log the url fine in the success, but I am trying to now append parameters to the url and they are being ignored. 我正在使用一个测试.json文件,并且可以在控制台上成功记录url,但我现在尝试将参数附加到url并且它们被忽略。

This is what the view looks like, works fine and success message appears with a test json file: 这是视图的样子,工作正常,成功消息与测试json文件一起出现:

busImportSearch: function() {
    importSelect.fetch({
        data: {
            importPhone: '5555555555',
            busImportName: 'test business',
            busImportCID: '12345',
            busImportBID: '1234567890'
        },
        success: function(results) {
            // url console.log's fine just no params
        }
    });
}

This is what I have in the collection: 这就是我在集合中的含义:

var importSelect = Backbone.Collection.extend({
    model: importModel,

    url:'somepath/test.json',

    sync: function(method, model, options) {
        options.timeout = 10000;
    options.dataType = "json";
        return Backbone.sync(method, model, options);
    },

    parse: function(response) {
        console.log(this.url);
        if (typeof response.data !== 'undefined') {
            this.result = response.data.list;
         }
        return this.result;
     },
});

 return new importSelect;
});

Edit 编辑

I think this is working but I think there is a better way to do this: 我认为这是有效的,但我认为有更好的方法:

url: function() {
  var busimportPhone = $("#busImportPhone").val();
  var busImportName = $("#busImportName").val();
  var busImportCID = $("#busImportCID").val();
  var busImportBID = $("#busImportBID").val(); 
  var updateUrl = 'test.json' + '?importPhone=' + busimportPhone + '&importName=' + busImportName + '&importCID=' + busImportCID + '&importBID' + busImportBID; return updateUrl;
},

This is not a whole lot better, but you could store the list of properties on the collection some where and create the url from there. 这不是更好,但您可以将集合中的属性列表存储在某处,并从那里创建URL。 Eg. 例如。

importSelect.myDataList = {
        importPhone: '5555555555',
        busImportName: 'test business',
        busImportCID: '12345',
        busImportBID: '1234567890'
    };
importSelect.fetch({ ... });

And construct your url using a function something like (code not tested) 并使用类似的函数构建您的URL(代码未测试)

function constructUrl(data) {
    var result = "";
    var count = 0;
    for(var i in data) {
       var prefix = "&";
       if(count == 0) {
           prefix = "?";
       }
       result += prefix + i + "=" + data[i];
    }
    return result;
}

and use function to create Url 并使用函数来创建Url

...
url: function() { 
    return constructUrl(this.data);
}

hope that helps. 希望有所帮助。

I think you're on the right path by defining the url function, just grab those query parameters from your collection, not directly from your page. 我认为您通过定义url函数走在正确的道路上,只需从您的集合中获取这些查询参数,而不是直接从您的页面中获取。 The collection should be able to know what kind of items are loaded, so it makes sense to store query parameters with the collection instance somehow. 该集合应该能够知道加载了哪种项目,因此以某种方式将查询参数与集合实例一起存储是有意义的。 In the past I've used an object in the Collection definition. 在过去,我在Collection定义中使用了一个对象。 But be careful, or you might end up with an object that's shared by all of those collection instances. 但是要小心,否则你最终会得到一个由所有这些集合实例共享的对象。

Side Note: I've been tempted before to set things up such that for any collection that could be generated from querying by various query parameters there would exist an associated model that only modeled the query parameters. 侧注:我之前一直试图设置,以便对于可以通过各种查询参数查询生成的任何集合,将存在仅模拟查询参数的关联模型。 Via event binding between the model and the collection, changing values of the model then could automatically trigger a fetch in the collection. 通过模型和集合之间的事件绑定,更改模型的值然后可以自动触发集合中的提取。 But would that be a reasonable trigger? 但这是一个合理的触发器吗? I'm not sure. 我不确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM