未在流星应用中定义的骨干
问题描述:
我一直在研究Meteor应用,并希望通过Backbone的路由功能添加多页功能。然而,当我这样做:未在流星应用中定义的骨干
meteor add backbone
meteor add underscore
,然后尝试将其与消息崩溃的应用程序中创建一个简单的“Hello World”:
ReferenceError: Backbone is not defined
at app/backbone.js:33:2
at run (/Users/timj/Documents/backbone/.meteor/local/build/server/server.js:142:63)
at Array.forEach (native)
at Function._.each._.forEach (/usr/local/meteor/lib/node_modules/underscore/underscore.js:79:11)
at run (/Users/timothyjaeger/Documents/backbone/.meteor/local/build/server/server.js:142:7)
Exited with code: 1
不知道我做错了,因为我已经增加了我的流星应用程序的骨干! JS的是这样的:
骨干试验app.js
if (Meteor.isClient) {
var AppView = Backbone.View.extend({
// el - stands for element. Every view has a element associate in with HTML content will be rendered.
el: '#container',
// It's the first function called when this view it's instantiated.
initialize: function(){
this.render();
},
// $el - it's a cached jQuery object (el), in which you can use jQuery functions to push content. Like the Hello World in this case.
render: function(){
this.$el.html("Hello World");
}
});
var appView = new AppView();
}
if (Meteor.isServer) {
Meteor.startup(function() {
Backbone.history.start({pushState: true});
// code to run on server at startup
});
}
答
骨干是一个客户端框架。您只需将代码从if (Meteor.isServer) {..}
中移除并将其放入if (Meteor.isClient) {..}
,即可将代码移到客户端。
如果使用客户端和服务器文件夹对代码进行分区,则不需要使用if (Meteor.isServer) {..}
或if (Meteor.isClient) {..}
,只需将与骨干相关的代码放入客户端文件夹即可。
我觉得待办事项应用程序使用骨干,如果不看看这个问题如何使用它在客户端:
How do I create dynamic URL's with Meteor?
也有流星了一个叫做meteor-router
这确实也是页面之间路由的绝佳工作,这也可能更适合您的需求。你可以找到更多的信息:
骨干可以在服务器上也可以使用! –
它可以??我很迷茫,如何可能?我知道它的更多抽象的部分(模型等)可以在服务器上运行,但我不认为它的路由位可以 – Akshat
那么一些客户端特定的东西像路由器,历史等在服务器上确实无法工作。但是,您可以使用主干模型/集合在服务器上构建数据。 –