简体   繁体   English

如何制作我可以包含在backbone.js项目中的配置文件

[英]How to make a config file which I can include in my backbone.js project

I want to make a default config file according to which I will create my view. 我想创建一个默认的配置文件,我将创建我的视图。

I am thinking of something like: 我想的是:

var Application = {};
Application.Config = {
   ViewerModule : {
     width    : '60%',
     height   : '60%',
     maxWidth : '99%',
     minWidth : '1%',
     iconSize : '24*24',
     defaultColor   : 'Green',
     selectedColor  : 'Orange',
     fontColor      : 'Black',
     viewerToolColor: 'White',
     defaultView    : 'Fit To Screen',
     Labels:{
            btnZoomIn     :'Zoom In',
            btnZoomOut    :'Zoom Out',
            btnRotateLeft :'Rotate Left',
            btnRotateRight:'Rotate Right',
            btnFitToScreen:'Fit to Screen',
            btnFullScreen :'Full Screen',
            btnSaveAs     :'Zoom In',
            btnExport     :'Zoom Out',
            btnPopOut     :'Rotate Left',
            btnEmail      :'Rotate Right',
            btnPdfConverter:'Fit to Screen',
            btnSetting    :'Settings'           
     }
   }
}

And so when I create my view in backbone, I can use this config value to define default values of my Backbone View. 因此,当我在骨干网中创建视图时,我可以使用此配置值来定义我的Backbone View的默认值。

One thing I thought was save values from config file to a backbone model and create a view with that model. 我想到的一件事是将配置文件中的值保存到主干模型并使用该模型创建视图。

However, I am not sure if this is the right thing. 但是,我不确定这是否是正确的。

Can share me your thoughts or examples on how I can achieve it. 可以与我分享您的想法或实例,告诉我如何实现它。

Have you considered using inheritance to overcome this problem? 您是否考虑过使用继承来克服这个问题? Instead of a config you could have a BaseView that has the aforementioned properties as view options . 您可以使用具有上述属性作为视图optionsBaseView ,而不是配置。 This way, these values could be overwritten in the implementation of your child view, or parsed in during construction of the child view. 这样,这些值可以在子视图的实现中被覆盖,或者在构建子视图期间进行解析。

Here's a crude example: 这是一个粗略的例子:

var BaseView = Backbone.View.extend({
    initialize: function() {
        this.options = {
            'Example': 'Foobar',
            'OverrideMe': 'Moo'
        };
    }
})

, ChildView = BaseView.extend({
    initialize: function() {
        this.options.Example = 'Something else';  
    }
})

, impl = new ChildView({'OverrideMe': 'Another thing'});

Here's a fiddle that shows it working. 这是一个表明它有效的小提琴

If I understood you, i suggest You use Model defaults , and here you can see simple example. 如果我理解你,我建议你使用模型默认值 ,在这里你可以看到简单的例子。 Or you can use your config like json object, but by the way you must to create empty Model and set config json to model(=new Model) 或者您可以像json对象一样使用您的配置,但是您必须创建空模型并将config json设置为model(= new Model)

You could mix in your config object into the prototype of your view with _.defaults if you want to set defaults for your views, with _.extend if you prefer to force the values. 如果要为视图设置默认值,可以使用_.defaults将配置对象混合到视图原型中,如果您希望强制使用_.extend

For example, 例如,

var Application = {};
Application.Config = {};
Application.Config.ViewerModule = {
    width: '60%',
    height: '60%'
};

var V = Backbone.View.extend({
    width: '50%'
});
_.defaults(V.prototype, Application.Config.ViewerModule);

var v = new V();
console.log(v.width, v.height);

And a demo http://jsfiddle.net/nikoshr/VX7SY/ 和演示http://jsfiddle.net/nikoshr/VX7SY/

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

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