从JSON填充数据以进行下拉 [英] Populating data from JSON for drop downs

查看:96
本文介绍了从JSON填充数据以进行下拉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从看起来像

{
    "accountType": ["Full","Trial"],
    "states": [
        {"state":"AL","stateDescription":"Alabama","featured":"A1"},
        {"state":"AK","stateDescription":"Alaska","featured":"B1"}
    ],
    "dates":[
        {"dateDescription":"Jan","month":1,"year":2008},
        {"dateDescription":"Feb","month":2,"year":2008}
    ]
}

我正在做的骨干文件:

define([ 'backbone', 'underscore', 'vent', ], function (Backbone, _, vent) {
    'use strict';

    return Backbone.Model.extend({

        url: {},

        loaded: false,

        defaults: {
            accountType: [],
            states: [],
            dates: [],
        },


        initialize: function () {
            this.on('change', this.change);
            var that = this;

            $.getJSON("webapp/jsondata", function (data) {
                that.set({
                    states: data.states.state,
                });
            });

            $.getJSON("webapp/jsondata", function (data) {
                that.set({
                    dates: data.dates.dateDescription,
                });
            });

            $.getJSON("webapp/jsondata", function (data) {
                that.set({
                    accountType: data.accountType,
                });
           });
        },  
    });
});

因此每个$.getJSON应该获取相关数据并填充主干模型默认值.

So each $.getJSON should get the relevant data and populate the backbone model defaults.

当前只有account类型有效.我不明白为什么这样行得通,而其他人则行不通,因为这是相同的代码.唯一的区别在于JSON数据,accountType具有2条数据. States有3个,我只想退还其中的一个(state).

Except at the moment only the account type works. I don't understand why this would work and the others wouldn't as it's the same code. The only difference is in the JSON data, accountType has 2 pieces of data. States has 3 and I only want to return one piece of this (state).

所以我想我的问题是在$.getJSON代码中指定要检索的数据,但许多小时的在线时间却没有给出答案.

So I guess my issue is in specifying the data being retrieved in the $.getJSON code but many hours online have not revealed an answer.

推荐答案

您数据中的statesdates键是数组,但是您尝试将它们作为散列来访问. 如果要从状态数组中提取状态键,则可以使用 _.pluck :

Your states and dates keys in your data are arrays, but you try to access them as hashes. If you want to extract the state keys from your states array, you can use _.pluck :

$.getJSON("webapp/jsondata", function (data) {
    that.set({
        states: _.pluck(data.states, 'state')
    });
});

在给出数据的情况下,_.pluck(data.states, 'state')将为您提供["AL", "AK"]

With the data given, _.pluck(data.states, 'state') will give you ["AL", "AK"]

并非如此,按照书面规定,将model.fetch model.parse .例如:

Not that, as written, your model could be greatly simplified by using model.fetch with model.parse. For example :

return Backbone.Model.extend({
    url: "webapp/jsondata",
    loaded: false,
    defaults: function () {
        return {
            accountType: [],
            states: [],
            dates: [],
        };
    },

    parse: function (data) {
        return {
            accountType: data.accountType,
            states: _.pluck(data.states, 'state'),
            dates: _.pluck(data.dates, 'dateDescription')
        };
    },

    initialize: function () {
        this.on('change', this.change);
        this.fetch();
    },  
});

当心在defaults哈希中使用数组,请改用函数.

And beware using arrays in a defaults hash, use a function instead.

这篇关于从JSON填充数据以进行下拉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆