骨干网和最佳实践越来越配置JSON [英] Backbone and best practice getting config JSON

查看:122
本文介绍了骨干网和最佳实践越来越配置JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON文件看起来像这样。

I've got a JSON file that looks like this.

{   
    "config": {
        "setting1": 'blabla',
        "setting2": 'blablabla'
    },
    "content": {
        "title": "Title of an exercise.",
        "author": "John Doe",
        "describtion": "Exercise content."
    },
    "answers": [
        {
            "id": "1",
            "content": "Dog",
            "correct": true
        },
        {
            "id": "2",
            "content": "Fish",
            "correct": false
        }
    ]
}

比,我创建了一个骨干视图,从内容模型相结合,和答案(这是随机选择的,但现在不是最重要的)。

Than, I create a Backbone View, combined from content model, and answers (which are randomly selected, but It's not most important now).

我也得到了一个配置,其中有设置,将定要使用的视图和收集方法。

I've also got a config, which has settings that will determinate which view and collection methods to use.

这似乎是一个简单的任务,但我是新来的骨干,我不知道这是获取JSON文件的最佳途径,创建具有URL以JSON一个模型,也比使用解析和初始化创建另一个模型,集合(附答案),或使用$ .getJSON方法,将创建正是我需要的型号?

It seems like a simple task, but as I'm new to Backbone, I'm wondering which is the best way to fetch JSON file, creating one model with url to JSON and than using parse and initialize creating another models and collections (with answers), or using $.getJSON method that will create exactly the models that I need?

使用$ .getJSON我试图

I was trying using $.getJSON

$.getJSON(source, function(data) {
    var contentModel = new ContentModel(data.content);
    var contentView = new ExerciseView({ model: contentModel });

    var answerCollection = new AnswersCollection();
    _.each(data.answers, function(answer) {
            answerCollection.add(answer);
    });

    var answersView = new AnswersView({collection: answerCollection});

    $(destination).html( contentView.render().el );
    $('.answers').append( answersView.el );
)};

但它看起来很优雅的解决方案不,我知道,这个应用程序需要良好的架构,因为它会基于'配置'其他许多意见进行开发。

But It doesn't seem very elegant solution, I know that this application needs good architecture, cause It will be developed with many other Views based on 'config'.

希望大家给我一些建议,有一个美好的一天!

Hope you guys give me some suggestions, have a good day!

推荐答案

我觉得你做了什么工作正常,是正确的。但是你可能需要重构一点点,因为这将基于'配置'许多其他的意见进行开发。

I think what you've done works fine and is correct. But you may need to refactor a little bit since "it will be developed with many other Views based on 'config'".

恕我直言,你需要做的第一件事就是处理失败在你的getJSON回调,使这一进程更加稳健。

IMHO, the first thing you need to do is to handle failure in your getJson callback to make the process more robust.

第二,它是有用的创建一个工厂来生成你的观点,因为你的逻辑是基于从服务器的配置数据不同的看法。所以工厂可能:

Second, it is useful to create a Factory to generate your views because your logic is to generate different views based on the config data from server. So the factory maybe:

contentViewFactory.generate = function(data) {
    var config = data.config;
    ....
    var ActualContentView = SomeContentView;
    var contentModel = new ContentModel(data.content);
    return = new ActualContentView({ model: contentModel });
}

如果你的逻辑很简单,你可以从一个配置字典地图可以观看像类:

If your logic is simple, you can have a dict map from config to view class like:

var viewMaps = {
    "exercise" : ExerciseView,
    "other": SomeOtherView,
    //....
}

如果每个工作流有AnswersView你可以保持在你的getJSON回调。因此,也许现在你的getJSON看起来是这样的:

And if every workflow has a AnswersView you can keep that in your getJSON callback. So maybe now your getJSON looks like this:

$.getJSON(source, function(data) {
    // keep the config->view logic in the factory
    var contentView = contentViewFactory.generate(data);

    var answerCollection = new AnswersCollection();
    _.each(data.answers, function(answer) {
        answerCollection.add(answer);
    });

    var answersView = new AnswersView({collection: answerCollection});

    $(destination).html( contentView.render().el );
    $('.answers').append( answersView.el );
})
.fail(){
    //some failure handling
};

此外,如果你有你的内容查看的共同逻辑,这是自然的,你可以有一个BaseContentView或ContentViewMixin提取共同的逻辑和应用扩展,使您的code更OO:

Furthermore, if you have common logics in you "ContentView"s, it's natural that you can have a "BaseContentView" or "ContentViewMixin" to extract the common logic and use extends to make your code more OO:

Backbone.View.extend(_.extend({}, ContentViewMixin, {
    //.....
}

所以,如果有人试图添加一个新的内容查看,他/她只需要添加一些code在出厂前要通过配置来产生新的看法。然后扩展ContentViewMixin实施新的视图。

So if someone is trying to add a new ContentView, he/she just needs to add some code in the factory to make the new View be generated by config. Then extends the ContentViewMixin to implement the new View.

这篇关于骨干网和最佳实践越来越配置JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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