简体   繁体   English

为什么在主干的字符串数组中的字符串数组转换

[英]Why Array of string transform in string array in backbone

I'm trying to do one service with backbone in wordpress, this have to fetch one url, and get from the url this values: 我正在尝试在wordpress中使用主干做一项服务,这必须获取一个网址,并从网址中获取以下值:

["C:\\wamp\\www\\wordpress\/wp-content\/uploads",
"C:\\wamp\\www\\wordpress\/wp-content\/uploads\/2014",
"C:\\wamp\\www\\wordpress\/wp-content\/uploads\/2014\/07",
"C:\\wamp\\www\\wordpress\/wp-content\/uploads\/2014\/08",
"C:\\wamp\\www\\wordpress\/wp-content\/uploads\/2014\/10",
"C:\\wamp\\www\\wordpress\/wp-content\/uploads\/2014\/11",
"C:\\wamp\\www\\wordpress\/wp-content\/uploads\/woocommerce_uploads"]

i stock this information in one collection, but the problem is that backbone take each one of this put in models (good in this part) but if i ask for each model the value, i getting something like 我将这些信息存储在一个集合中,但是问题是主干采用了每个模型(在这一部分中都很好),但是如果我要求每个模型的值,我会得到类似

0:"c"
1:":"
2:"/"
.
.

I dont know why is this happen and how to fixed, any of you know the why of this problem? 我不知道为什么会这样以及如何解决,你们中的任何人都知道为什么这个问题吗? and the solution? 和解决方案?

This is my collection 这是我的收藏

var CCUFFolder= Backbone.Collection.extend({
    url:ajaxurl
});

And the fetch 和获取

cCUFFolder.fetch({type:'POST',data:{action: "cuf_get_dirs"}});

Backbone's Model type is essentially a wrapper around object literals. 骨干的Model类型本质上是对象文字的包装。 As you are constructing your models from strings, Backbone is creating each model's attributes hash by parsing each string for key/value pairs - which is producing an array of characters. 当你从字符串构建你的模型,骨干是建立每个模型的attributes这是生产的字符数组-通过分析每个字符串键/值对哈希值。 What you want to do is to construct your model's from object literals: 您要做的是从对象文字构造模型:

var collection_source = [
    { "Path": "C:\\wamp\\www\\wordpress\/wp-content\/uploads" },
    { "Path": "C:\\wamp\\www\\wordpress\/wp-content\/uploads\/2014" },
    { "Path": "C:\\wamp\\www\\wordpress\/wp-content\/uploads\/2014\/07" },
    { "Path": "C:\\wamp\\www\\wordpress\/wp-content\/uploads\/2014\/08" },
    { "Path": "C:\\wamp\\www\\wordpress\/wp-content\/uploads\/2014\/10" },
    { "Path": "C:\\wamp\\www\\wordpress\/wp-content\/uploads\/2014\/11" },
    { "Path": "C:\\wamp\\www\\wordpress\/wp-content\/uploads\/woocommerce_uploads" }    
];

var folder = new CCUFFolder(collection_source);

console.log(folder.first().get('Path'));

You should implement parse method in your collection: 您应该在集合中实现parse方法:

var CCUFFolder= Backbone.Collection.extend({
    url: ajaxurl,

    parse: function (response) {
        return _.map(response, function (item) {
            return { path: item }
        });
    }
});

After that you can retrieve models correctly: 之后,您可以正确检索模型:

cCUFFolder.each(function (model) {
   console.log(model.get('path');
});

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

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