【发布时间】:2015-09-22 03:10:16
【问题描述】:
我正在关注这个网站上的教程:
http://code.tutsplus.com/tutorials/working-with-data-in-sailsjs--net-31525
我被困在最后一部分,利用网络套接字。
最初我输入了代码,但收到了 javascript 错误:
Uncaught TypeError: this.socket.request is not a function
所以我决定复制并粘贴教程的代码,但它给出了同样的错误。
这是代码块:
var SailsCollection = Backbone.Collection.extend({
sailsCollection: "",
socket: null,
sync: function(method, model, options){
var where = {};
if (options.where) {
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(users);
}, 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");
}
}
});
我相信这个教程可能已经过时了,但我仍然希望我能修复最后一点,尤其是一路走来。
有人知道在这个 BackboneJS 代码中使用 Sailsjs 套接字的正确方法吗? (我知道我在这里大海捞针)。
更新 1:
在 freenode IRC 上与 #Sailjs 人员的一些初步讨论表明 Grunt 任务没有运行,因此 Sails.js 没有注入 Socket.io?
更新 2:
好的,这里似乎已经提出了一个重复的问题:
Sails is not injecting the files within the assets folder
投票结束我自己的问题,帮我投赞成票:)
【问题讨论】:
标签: backbone.js websocket socket.io sails.js