1

I'm staring with Knockout.js and I didn't find much documentation about how to properly structure a knockout application.

It is easy to follow the tutorial from the docs and its multiple examples in them or in other pages, but there's no much about the good practices regarding the file structure.

I've seen some videos of Steve Sanderson talking about how to architect a big project but they seem to be going a bit too far me. He mentions the Yeoman tool to generate a basic KO structure, but I'm using Node.js with Express.js and I already have another structure I'm working with and I'm not quite sure about how to mix both.

What I'm having at the moment is 3 main files:

  • functions.js
  • viewmodels.js (ko viewmodels and domain classes)
  • events.js (for jQuery events)

As you can see, the viewmodels.js file will become bigger and bigger so I was thinking about separating each viewmodel with its associated domain classes in different files.

The problem I found is that some of my viewmodels are related with each other as they have to access to each other data at some point.

I'm making use of a mastermodel at the moment for it:

var MasterModel = function(){
    this.user = new UserViewModel();
    this.department = new DepartmentViewModel();
}

var mm = new MasterModel();
ko.applyBindings(mm);

So I can do things such as mm.user.sayHi() from a departmentViewModel

Any suggestions regarding the structure issue?

4

1 回答 1

1

好吧,Yeoman 工具使用 CrossRoadsJS 和 RequireJS 生成单页应用程序结构。由于您使用的是 ExpressJS,我认为 Yeoman 结构不适合您的情况。

如果您的视图模型越来越大,继承是不错的选择。

看看这篇文章,很不错,并且教了如何让javascript继承,包括私有和公共的方法和属性。您可以使用它来创建强大且可重用的视图模型。

关于好的做法,这个博客非常好。它包含几篇关于 ko 组件和许多其他内容的文章。

希望这有帮助!

于 2015-08-13T14:54:11.377 回答