2013-07-16 51 views
0

我正在编写一个使用dojo的fontend web应用程序,该应用程序使用xhr执行大量调用来休息端点。我想有一个地方来存储配置,例如端点位置和html标记引用。我以为我会用xhr调用一个json文件来做到这一点,但我无法让我的函数以正确的顺序触发。下面是我的主要js文件,它有一个init()函数,我将其作为回调传递给我的conf初始化程序(“ebs/conf”)模块,也在下面。我使用Chrome调试器在我的conf.get()方法中设置了断点,看起来好像它永远不会被调用。使用json文件来存储dojo应用程序的配置

有人可以给我一些建议吗?

主要JS文件:

// module requirements 
require([ "dojo/dom", "dojo/on", "ebs/prices", "ebs/cart", "ebs/conf", 
    "dojo/ready" ], function(dom, on, prices, cart, conf, ready) { 

ready(function() { 

    conf.get("/js/config.json", init()); 

    function init(config) { 

     on(dom.byId("height"), "keyup", function(event) { 
      prices.calculate(config); 
     }); 
     on(dom.byId("width"), "keyup", function(event) { 
      prices.calculate(config); 
     }); 
     on(dom.byId("qty"), "keyup", function(event) { 
      prices.calculate(config); 
     }); 
     on(dom.byId("grills"), "change", function(event) { 
      prices.calculate(config); 
     }); 

     cart.putSampleCart(); 
     cart.load(config); 

    } 

}); 

}); 

这里是我的 '的conf' 模块( “EBS/conf目录”):

define(["dojo/json", "dojo/request/xhr"], function(json, xhr) { 
return { 
    get : function(file, callback) { 
     // Create config object from json config file 
     var config = null; 
     xhr(file, { 
      handleAs : "json" 
     }).then(function(config) { 
      callback(config); 
     }, function(error) { 
      console.error(error); 
      return error; 
     }); 
    } 
} 
}); 

回答

1

你没有传递函数作为回调。您正在执行它并将结果作为第二个参数传递。

conf.get("/js/config.json", init()); 

应该

conf.get("/js/config.json", init); 
相关问题