Backbone/RequireJS 和多种模型
我很难把我的注意力主要集中在 RequireJS 上。我认为这是一项很好/必要的技术,但对我来说,实施它确实是一项艰巨的任务。我非常感谢你的帮助!
我正在尝试使用 Backbone 和 RequireJS 开发一个相当灵活的应用程序。问题是我完全习惯了像 new Person()
这样的语法,而不必指定依赖项。有没有一种有效的方法可以将 RequireJS 与相当多的模型一起使用?我认为我的问题始终是处理退货。我考虑过使用工厂方法来创建带有 require
函数的模型,但这样做需要 require
函数是同步的,这完全违背了 RequireJS 的目的。
必须首先需要我的所有模型,然后将它们包含在实例化函数中,这似乎并不正确 - 或者我是这样?
您对如何构建和建模这样的应用程序有什么建议或教程吗?
谢谢你帮我!
最大J
I am having trouble wrapping my mind around primarily RequireJS. I see that it is a good/necessary technology, but implementing it, for me, has been a real stretch. I greatly appreciate your help!
I am trying to develop a fairly flexible application with Backbone and RequireJS. The problem is that I am totally used to syntax like new Person()
without having to specify dependencies. Is there an efficient way to use RequireJS with quite a number of models? I think my problem is always working with returns. I considered using a factory method to create the model with the require
function, but doing so necessitates that the require
function is synchronous, which completely defeats the purpose of RequireJS.
It just doesn't seem right to have to require all of my models first and then include those in the instantiation function - or do I?
Do you have any suggestions or tutorials about how to structure and model an application like this?
Thank you for helping me out!
JMax
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用我所说的 require js 模块模式。如果您知道一组类经常一起使用,您可以执行类似的操作。
首先,您在单独的文件中定义每个类,然后定义一个模块将它们组合在一起
然后,当您想使用此模块中的类时,您只需这样做
我不相信使用用 requirejs 定义的模块我们应该返回一个实例 - 我们应该总是返回一个构造函数或一个对象。
您应该完全接受定义和要求 - 随着时间的推移,您会开始喜欢它 - 无需过多考虑手动添加/跟踪依赖项等,或者(所以 2005 年!)将大部分内容放在一个文件中:)
You can use what I call require js module pattern. If you know a group of classes are often used together you can do something like this.
First you define each class in a separate file and then you define a module to hold them together
And then when you want to use class from this module you just do
I don't believe using modules defined with requirejs we should return an instance - we should always return a constructor or an object.
You should totally embrace the defining and requiring - with time you'll start to love it - without having to think much about adding/tracking dependencies etc. everywhere by hand or (so 2005!) having most of the stuff in one file :)