1

我一直在开发 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
      });
    }
4

1 回答 1

0

Backbone 是一个客户端框架。您只需通过从 中删除与主干相关的代码if (Meteor.isServer) {..}并将其放入if (Meteor.isClient) {..}.

如果您使用客户端和服务器文件夹来划分您不需要使用if (Meteor.isServer) {..}或的代码if (Meteor.isClient) {..},您只需将主干相关代码放入客户端文件夹中。

我认为 todos 应用程序使用主干,如果没有,请查看如何在客户端使用它的这个问题:

如何使用 Meteor 创建动态 URL?

还有一个名为 meteor 的包meteor-router,它也可以在页面之间进行路由,这也可以更舒适地满足您的需求。您可以在以下位置找到更多信息:

https://github.com/tmeasday/meteor-router

于 2013-01-12T22:08:07.773 回答