Requirejs - 使用单例对象,无法获取返回的模板

时间:2013-05-09 14:30:46

标签: backbone.js requirejs

我正在启动一个应用程序(我是requirejs的新手)requirejs app的单例模型...基本上我将模板数组传递给我的实用程序js,并将模板分配给适当的视图......

我用:

app.js,singleton.js(返回对象),index.js用于实现索引页面模板,用于将模板分配给适当视图的实用程序......

但是在index.js中,我控制了this.template ...我得到一个结果为“function(n){return e.call(this,n,w)}” - 我理解我的方法是错的..可以任何一次给我正确的方式给我..或者突出我错了我做什么pelase ..?

先谢谢..

这是我的所有js文件:

app.js: -

requirejs.config({
    baseUrl: 'js',
    paths: {
        "jquery"    : 'lib/jquery-1.9.1.min',
        "underscore": "lib/underscore-min",
        "backbone"  : "lib/backbone-min",
        "singleton" : "utils/singleton",
        "model"     : "models/model",
        "index"     : "views/index",
        "utils"     : "utils/utils",
    },
    shim:{
        "underscore":{
            exports: '_'
        },
        "backbone":{
            exports: 'Backbone',
            deps:['underscore']
        },
        "utils" : {
            deps:['index'],
            deps:['model']
        }
    }
});

require(["jquery","underscore","backbone","singleton","utils","index"],function ($,_,Backbone,obj) {
    obj.makeTemp(['index']);
});

singleton.js(只返回对象)

define(function () {
    var EDMS = EDMS || {};
    return EDMS;
})

utilities.js(我指定模板)

define(["underscore", "singleton","model","index"],function (_,obj,model) {

    var EDMS = obj || {};

        EDMS.makeTemp = function(views){
            $.each(views, function(index,view){
                if(view){
                        var temp = $.get('templates/' + view + '.html')
                        .done(function(data){
                            EDMS[view].prototype.template = _.template(data);
                            new EDMS.index;
                        })
                }else{
                        console.log("No template found!")
                    }

                });
        }

    return EDMS;

})

最后我的index.js,我应该假设得到模板,由utilities.js分配

define(["singleton","underscore","backbone","utils"],function (obj,_,Backbone) {

        var EDMS = obj || {};

        EDMS.index = Backbone.View.extend({
            initialize:function(){
                console.log(this.template);
                            error i am getting as "function (n){return e.call(this,n,w)} "
            }
        });

})

1 个答案:

答案 0 :(得分:0)

这对我有用:

define(['singleton',"underscore","backbone"],function (obj,_,Backbone) {

    window.EDMS = obj || {};

    var temps = function(views){
        var tempts = [];
        $.each(views, function(i,view){
            var data = $.get("templates/"+views+".html");
            data.done(function(res){
                EDMS[views].prototype.template = _.template(res);
                new EDMS.index;
            });
        })
    };
    return temps;
});

谢谢大家。