Zend framework Durandal:如何将配置数据从HTML主机传递到ViewModel?
我正在使用Zend生成我的主视图主机。因此,它是唯一在服务器端被解析的HTML。我的服务器知道一个配置参数,我想一直传递给我的一个viewmodels。我不希望viewmodel通过ajax请求这些数据 如何让视图通过main.js,通过shell,向下传递数据到durandal中的viewmodel 现在,我正在设置一个全局变量中的值,然后在我的index.phtml中的viewmodels中引用该全局变量:Zend framework Durandal:如何将配置数据从HTML主机传递到ViewModel?,zend-framework,mvvm,knockout.js,viewmodel,durandal,Zend Framework,Mvvm,Knockout.js,Viewmodel,Durandal,我正在使用Zend生成我的主视图主机。因此,它是唯一在服务器端被解析的HTML。我的服务器知道一个配置参数,我想一直传递给我的一个viewmodels。我不希望viewmodel通过ajax请求这些数据 如何让视图通过main.js,通过shell,向下传递数据到durandal中的viewmodel 现在,我正在设置一个全局变量中的值,然后在我的index.phtml中的viewmodels中引用该全局变量: <script> //This sucks, b
<script>
//This sucks, but i don't know how to pass stuff down into Durandal yet...
//
window.ServiceRoot = "<?=$this->contentRoot?>";
</script>
//这很糟糕,但我还不知道如何把东西传给杜兰达尔。。。
//
window.ServiceRoot=“”;
在一个直接的KO应用程序中,我会把它传递给koviewmodel构造函数(或者设置一个可观察的属性)
从技术上讲,我使用的是durandal 2.0预发行版,但我认为这并不重要。我想我需要像传递主参数一样通过require.js脚本标记传递参数。我建议您添加一个config.js模块来保存“配置”数据。添加一个initialize函数以从服务器获取配置数据并将其缓存 然后。。。在shell.js的activate函数中,在绑定视图之前初始化配置 然后,您可以在所有viewmodels中要求配置模块,它将只返回缓存的数据 config.js
define(['dataaccessmodule'], function (dataaccessmodule) {
var config =
{
serviceRoot: null,
init: init
};
var init= function()
{
// get config from server and set serviceRoot;
// return a promise
};
return config;
});
define([... your required modules..., 'config'],
function (..., config) {
function activate() {
return config.init().then(boot);
};
function boot() {
// set up routing etc...
// activate the required route
return router.activate('home');
};
});
define([... your required modules..., 'config'],
function (..., config) {
var someViewModel =
{
serviceRoot: config.serviceRoot
};
return someViewModel;
});
shell.js
define(['dataaccessmodule'], function (dataaccessmodule) {
var config =
{
serviceRoot: null,
init: init
};
var init= function()
{
// get config from server and set serviceRoot;
// return a promise
};
return config;
});
define([... your required modules..., 'config'],
function (..., config) {
function activate() {
return config.init().then(boot);
};
function boot() {
// set up routing etc...
// activate the required route
return router.activate('home');
};
});
define([... your required modules..., 'config'],
function (..., config) {
var someViewModel =
{
serviceRoot: config.serviceRoot
};
return someViewModel;
});
someViewModel.js
define(['dataaccessmodule'], function (dataaccessmodule) {
var config =
{
serviceRoot: null,
init: init
};
var init= function()
{
// get config from server and set serviceRoot;
// return a promise
};
return config;
});
define([... your required modules..., 'config'],
function (..., config) {
function activate() {
return config.init().then(boot);
};
function boot() {
// set up routing etc...
// activate the required route
return router.activate('home');
};
});
define([... your required modules..., 'config'],
function (..., config) {
var someViewModel =
{
serviceRoot: config.serviceRoot
};
return someViewModel;
});
我知道您说过您不想通过ajax加载数据,但是使用这种方法,您只需加载一次并重新使用它。如果需要,还可以加载额外的配置。这使用单一责任原则很好地分离了代码
编辑:
如果确实需要在呈现页面中执行此操作,可以按照以下方式执行:
<script>
var myapp = myapp || {};
myapp.config= (function() {
var contentRoot = "<?=$this->contentRoot?>";
return {
contentRoot: contentRoot
};
})();
</script>
然后,您可以像平常一样在viewmodel中需要appconfig模块,并使用appconfig.contentRoot访问contentRoot。thx以获得详细的响应,如果不是针对我的情况,它会工作得很好。但是,我无法通过ajax从服务器“获取”配置,因为配置本身就是服务器URL。应用程序托管在各种主机/路径/子路径上,因此,了解服务根url的唯一可靠方法是服务器本身。这就是为什么ajax技术对我不起作用的原因。如果可能的话,我想避免在JS中使用discover逻辑。您编辑的响应有点粗糙,但比我的超级全局黑客要少得多。您不能在主机页中正确定义吗?在典型场景中,
require.js
尚未加载,因此define
将是undefined
。您可以尝试在标题中加载require.js
,然后加载'define('appconfig',[],function(){return contentRoot=”“;})和最后一次加载main.js。@RainerAtSpirit:这可能行得通。不过,我不确定这对温泉会有什么影响。克里斯达莫:是的,这有点老套,但你要么这么做,要么采纳雷纳的建议。