我正在尝试将 jqplot 与 Durandal 和淘汰赛一起使用。我在 Durandal 组 ( https://groups.google.com/forum/#!topic/durandaljs/WXBiSK3WmIs ) 上找到了 Rob 的帖子,该帖子解决了这个问题,但它使用的视图模型结构完全让我感到困惑向构造函数添加“prototype.activate”方法,这对我来说是全新的(当我尝试使用它时不起作用)。
任何人都可以尝试告诉我如何将 Rob 的示例与我的以下显示模块模式一起使用?
我的视图模型:
define(['globalVar', 'services/datacontext', 'services/calccontext"], function (globalVar, datacontext, calcContext) {
var activate = function () {
return datacontext.newEntity(articleResults, "articleVersionResults");
};
var calcAll = function () {
//can be called externally and recalcs all
return individualImpactCalc('byWeightAndFactor', 'CO2e', articleResults().material_CO2e);
//will be more calls in here soon
};
var individualImpactCalc = function (calcName, fac, calcObservable) {
return calcContext.indCalc(calcName, fac, calcObservable, globalVar.components()); //puts result straight into observable
};
var vm = {
activate: activate,
calcAll: calcAll
};
return vm;
});
Rob 的代码示例:
define(["jquery", "knockout"], function($, ko){
// constructor
var ctor = function () {
var self = this;
// properties
self.chartConfig = {};
};
ctor.prototype.activate = function(id, page) {
var self = this;
// get data from database
self.chartConfig.data([
[300, 295, 290],
[320, 320, 320],
[260, 265, 260]
]);
self.chartConfig.title("Forecast net operating costs");
self.chartConfig.seriesColors(["#0026FF", "#FF6A00", "#606060"]);
self.chartConfig.series([
{ label: "Foo" },
{ label: "Bar" },
{ label: "Baz" }
]);
self.chartConfig.ticks(["Apr 13", "May 13", "Jun 13"]);
};
ctor.prototype.compositionComplete = function (view, parent) {
var self = this;
var c = self.chartConfig;
self.jqChart = $.jqplot("chart", c.data(), ...
// example to change the data
setTimeout(function () {
self.chartConfig.data([
[280, 280, 280],
[280, 280, 280],
[280, 280, 280]
]);
}, 4000);
};
return ctor;
});
编辑 1:问题是 compositioncomplete 没有触发。此视图模型是主页面中的子视图。不知道这是否会影响事情?
我的视图模型现在是:
define(['globalVar', 'services/datacontext', 'services/calccontext', "jquery", "knockout"], function (globalVar, datacontext, calcContext, $, ko) {
// constructor
var ctor = function () {
var self = this;
// properties
self.chartConfig = {
data: ko.observableArray([]),
title: ko.observable(),
seriesColors: ko.observableArray([]),
series: ko.observableArray([]),
ticks: ko.observableArray([])
};
// subscriptions
self.chartConfig.data.subscribe(function (newValue) {
var opts = {
data: newValue
};
if (self.jqChart) {
self.jqChart.replot(opts);
console.log("chart replotted", opts);
}
});
};
function setChartVars() {
var self = this;
// get data from database
self.chartConfig.data([
[300, 295, 290],
[320, 320, 320],
[260, 265, 260]
]);
self.chartConfig.title("Forecast net operating costs");
self.chartConfig.seriesColors(["#0026FF", "#FF6A00", "#606060"]);
self.chartConfig.series([
{ label: "Foo" },
{ label: "Bar" },
{ label: "Baz" }
]);
self.chartConfig.ticks(["Apr 13", "May 13", "Jun 13"]);
};
var activate = function () {
ctor();
setChartVars();
return datacontext.newEntity(articleResults, "articleVersionResults");
};
var compositionComplete = function () {
var self = this;
var c = self.chartConfig;
self.jqChart = $.jqplot("chart", c.data());
// example to change the data
setTimeout(function () {
self.chartConfig.data([
[280, 280, 280],
[280, 280, 280],
[280, 280, 280]
]);
}, 4000);
};
var calcAll = function () {
//can be called externally and recalcs all
//return res_mat_gwp();
return individualImpactCalc('byWeightAndFactor', 'CO2e', articleResults().material_CO2e);
};
var individualImpactCalc = function (calcName, fac, calcObservable) {
return calcContext.indCalc(calcName, fac, calcObservable, globalVar.components()); //puts result straight into observable
};
var vm = {
activate: activate,
compositionComplete: compositionComplete,
calcAll: calcAll,
editArticleVersion: globalVar.editArticleVersion,
articleResults: articleResults,
ctor: ctor
};
return vm;
});