SailsJS + Backbone:未定义Backbone

时间:2014-03-04 09:27:45

标签: jquery backbone.js sails.js

我遇到了麻烦,我正在尝试使用Sailsjs插入Backbone.js,但是当我启动应用程序时,我的JS控制台出错:

Error: http://localhost:1337/linker/js/backbone-min.js is being assigned a //# sourceMappingUrl, but already has one

TypeError: i is undefined (in backbone-min.js)

ReferenceError: Backbone is not defined

这是我的代码:

backbone_post_message.js:监听新帖子是否添加或更新并显示的js文件

 jQuery(document).ready(function() {
    var MessageModel = Backbone.Model.extend({
        rootUrl: '/messages'
    });

    var MessagesCollection = Backbone.Collection.extend({
        url: '/messages',
        model: MessageModel
    });

    var messages = new MessagesCollection();
    messages.fetch();

    jQuery("#submitMessage").on("click", function() {
        console.log("clicked !");
        var messageText = jQuery("#message").val();
        messages.create({message: messageText}, {wait: true});
        jQuery("#message").val("");
    });

    _.templateSettings = {
        interpolate: /\{\{(.+?)\}\}/g
    };

    var MessageView = Backbone.View.extend({
        el: "#messagesContainer",
        initialize: function() {
            this.collection.on('add', this.render, this);
            this.render();
        },
        template: _.template('<div><p><strong>{{ username }} :</strong> {{ message }}</p></div>'),
        render: function() {
            this.$el.html("");
            this.collection.each(function(message) {
                this.$el.append(this.template(message.toJSON()));
            }, this);
        }

    });

    var mView = new MessageView({collection: messages});

    var SailsCollection = Backbone.Collection.extend({
        sailsCollection: null,
        socket: null,
        sync: function(method, model, options) {
            where: {};
            if(options.where) {
                where: { options.where }
            }
            if(typeof this.sailsCollection === "string" && this.sailsCollection !== "") {
                this.socket = io.connect();
                this.socket.on("connect", _.bind(function() {
                    this.socket.request("/" + this.sailsCollection, where, _.bind(function(users) {
                        this.set(user);
                    }, this));
                    this.socket.on("message", _.bind(function(msg) {
                        var m = msg.uri.split("/").pop();
                        if(m === "create") {
                            this.add(msg.data);
                        } else if(m === "update") {
                            this.get(msg.data.id).set(msg.data);
                        } else if(m === "destroy") {
                            this.remove(this.get(msg.data.id));
                        }
                    }, this));
                }, this));
            } else {
                console.log("Error : Cannot retrieve models because property 'sailsCollection' not set on the collection");
            }
        }
    });

    var MessageCollection = SailsCollection.extend({
        sailsCollection: "messages",
        model: MessageModel
    });
});

MainController.js:我的应用程序的主控制器

    var MainController = {

  index: function(req, res) {
      res.view();
  },
  signup: function(req, res) {
      var username = req.param("username");
      var password = req.param("password");

      Users.findOne(username).done(function(error, user) {
          if(error) {
              res.send(500, { error: "DB Error" });
          } else if(user) {
              res.send(400, { error: "Username already exists"});
          } else {
              var hasher = require("password-hash");
              password = hasher.generate(password);

              Users.create({username: username, password: password}).done(function(error, user) {
                  if(error) {
                      res.send(500, { error: "DB Error"});
                  }

                  req.session.user = user;
                  res.send(user);
              });
          }
      });
  },
  login: function(req, res) {
      var username = req.param("username");
      var password = req.param("password");

      Users.findOne({username: username}).done(function(error, user) {
          if(error) {
              res.send(500, {error: "DB Error"});
          }

          if(user) {
              var hasher = require("password-hash");

              if(hasher.verify(password, user.password)) {
                  req.session.user = user;
                  res.send(user);
              } else {
                  res.send(400, {error: "Wrong password"});
              }
          } else {
              res.send(404, {error: "User not found"});
          }
      });
  },
  chat: function(req, res) {
      if(req.session.user) {
          res.view({username: req.session.user.username});
      } else {
          res.view('/');
      }
  },


  /**
   * Overrides for the settings in `config/controllers.js`
   * (specific to MainController)
   */
  _config: {}


};
module.exports = MainController;

此外,我的所有图书馆都加载在我的layout.ejs:

<script src="/linker/js/socket.io.js"></script>
<script src="/linker/js/sails.io.js"></script>
<script src="/linker/js/app.js"></script>
<script src="/linker/js/jquery-1.11.0.min.js"></script>
<script src="/linker/js/jquery.validate.min.js"></script>
<script src="/linker/js/backbone-min.js"></script>
<script src="/linker/js/backbone_post_message.js"></script>
<script src="/linker/js/login.js"></script>
<script src="/linker/js/signup.js"></script>
<script src="/linker/js/underscore-min.js"></script>

我不明白为什么会出现这些错误......

那你可以帮我解决这些错误吗?

先谢谢,

西里尔

1 个答案:

答案 0 :(得分:0)

将下划线文件放在主干上,因为主干依赖于下划线。