【问题标题】:Separating javascript "classes" or "modules" from View Model with Knockout.js / RequireJs使用 Knockout.js / RequireJs 从视图模型中分离 javascript“类”或“模块”
【发布时间】:2015-09-04 06:36:24
【问题描述】:

我有一个正在使用 Knockout 开发的应用程序,我正在使用 RequireJs,我想要做的是让某些代码可用于多个不同的页面,而不必将代码放在多个文件中...这样的事情:

    function perPageOption(value, display) {
        this.value = value;
        this.display = display;
    }

我的页面中有:

<script data-main="/Scripts/JournalEntriesDetailPage" src="~/Scripts/require.js"></script>

以及脚本(JournalEntriesDetailPage):

require(['Knockout', 'journalEntiresDetailViewModel', './app/JournalModels', 'domReady'], function (ko, viewModel) {
ko.applyBindings(new viewModel());

});

然后在我的“journalEntiresDetailViewModel”中有这个:

//per page options
self.perPageOptions = [new perPageOption(10, 10), new perPageOption(20, 20), new perPageOption(50, 50), new perPageOption(100, 100)];

'./app/JournalModels' 是数组/类所在的位置,当我尝试在尝试“导入”它们的任何页面中使用它们时,它们会出现“未定义”。我很难弄清楚这一点。我已经查看了 requirejs 上多页面应用程序的示例,对于我正在尝试做的事情来说,它们似乎太复杂了。我不明白为什么当我“要求”它时“domready”正在工作,但我的文件不工作。

我也试过了,但没有成功:

define(['Knockout'], function (ko) {
    define('EntryType', function () {
    function EntryType(data) {
        this.id = ko.observable(data.ID);
        this.name = ko.observable(data.Name);
        this.color = ko.observable(data.Color);
        this.journaltypeid = ko.observable(data.JournalTypeID);
    }
    return new EntryType();
    });
});

define('EntryType', function () {
    return function EntryType(data) {
        this.id = ko.observable(data.ID);
        this.name = ko.observable(data.Name);
        this.color = ko.observable(data.Color);
        this.journaltypeid = ko.observable(data.JournalTypeID);
    }
});

define(['Knockout'], function (ko) {

return {
    pagePerOption: function(data) {
        this.value = value;
        this.display = display;
    },
    entryType: function(data) {
        this.id = ko.observable(data.ID);
        this.name = ko.observable(data.Name);
        this.color = ko.observable(data.Color);
        this.journaltypeid = ko.observable(data.JournalTypeID);
    }
}
});

【问题讨论】:

  • 我并没有对它们做太多,但你见过Knockout Components吗?我认为他们可能会在这里为您提供帮助。
  • 你在导出构造函数吗?
  • 我还没有看到,看起来我可以将它用于一些东西,但我需要一个可重复使用的“条目”对象,我可以在任何我想要的地方使用它.
  • 我认为define 期待一个返回您想要导出的函数,在这种情况下是您的构造函数。
  • 我不需要看整个东西,只是想确保它实际上是用 requirejs 语法编写的,并以这种方式加载它的依赖项。

标签: javascript knockout.js requirejs


【解决方案1】:

好的,我以为我以前尝试过,但现在可以了:

页面:

    <script data-main="/Scripts/JournalEntriesDetailPage" src="~/Scripts/require.js"></script>

初始化脚本(JournalEntriesDetailPage):

require(['Knockout', 'journalEntiresDetailViewModel', 'domReady!'], function (ko, viewModel) {
     ko.applyBindings(new viewModel());
});

查看模型脚本(journalEntiresDetailViewModel):

define(['Knockout', './app/JournalModels'], function (ko, model) {
return function () {
    //per page options
    self.perPageOptions = [new model.perPageOption(10, 10), new model.perPageOption(20, 20), new model.perPageOption(50, 50), new model.perPageOption(100, 100)];
    //tons of other code
    }
});

以及“模型”脚本(JournalModels)中的定义:

define(['Knockout'], function (ko) {

function _entryType(data) {
    this.id = ko.observable(data.ID);
    this.name = ko.observable(data.Name);
    this.color = ko.observable(data.Color);
    this.journaltypeid = ko.observable(data.JournalTypeID);
}

return {
    entryType: function (data) {
        return new _entryType(data);
    },
    entry: function (data) {
        this.id = ko.observable(data.ID);
        this.createdby = ko.observable(data.CreatedBy);
        var datey = new Date(+data.CreatedDate.match(/\d+/)[0]);
        this.createddate = ko.observable(datey.toLocaleDateString() + " " + datey.toLocaleTimeString());
        this.content = ko.observable(data.Content);
        this.entrytype = ko.observable(new _entryType(data.EntryType));
        this.entrytypename = ko.observable(data.EntryType.Name);
        this.entrytypeid = ko.observable(data.EntryType.ID);
        this.journalid = ko.observable(data.journalid);
    },

    perPageOption: function (value, display) {
        this.value = ko.observable(value);
        this.display = ko.observable(display);
    }
}
});

我需要在模型脚本中返回数据,我认为我尝试过,但我肯定没有完成其他重要部分......这是:

define(['Knockout', './app/JournalModels'], function (ko, model) {...

... 在视图模型脚本中,所以我可以在我的视图模型中使用“model”,例如“model.pagePerOption”。基本上我只是偶然发现了正确的组合......因为我正在尝试一切并对其进行如此多的改变,以至于我需要的两件事可能不会同时存在,直到它们最终出现。

感谢您的回复... :)

【讨论】:

  • 如果您没有在entry 之外使用_entryType,您甚至不必费心导出它。 entry 可以访问它,因为它在上层函数的范围内。使用 AMD,您通常会为每个文件加载一个模块,因此更简洁的方法是将 perPageOption(如您的问题帖子中)与 entry 分开,还允许您在函数参数中以 perPageOption 的形式访问它,而不是作为model.perPageOption。至于 Knockout,组件需要一些时间来适应,但一旦你习惯了,它们对于这类事情来说真的很棒。
猜你喜欢
  • 2013-06-05
  • 2015-12-27
  • 2016-09-27
  • 2014-10-04
  • 2014-01-03
  • 2015-02-26
  • 1970-01-01
  • 2016-11-28
  • 2017-08-28
相关资源
最近更新 更多