スタックオーバーフローに関する私の最初の投稿。(そして英語は私の母国語ではありません)。
私はemberjsの使い方を学ぼうとしています。良いチュートリアルがないため、簡単ではありません。
そこで、チャットをコーディングすることにしました。nodejs と socket.io サーバーサイドを使用します。
HTML
<script type="text/x-handlebars">
<div class="page">
<div class="users">
</div>
<div class="messagebox">
{{#view App.TextField valueBinding="currentMsg" placeholder="your message"}}{{/view}}
<button {{action "sendMsg"}}>Send</button>
</div>
<div id="chatbox">
{{#collection contentBinding="App.MsgsController" tagName="ul"}}
<b>{{value}}</b>
{{/collection}}
</div>
</div>
</script>
Javascript
var id;
var socketio = io.connect("127.0.0.1:8888");
socketio.on('id', function (data) {
id = data;
});
socketio.on("broadcast", function (data) {
if (id == data.id) {
return
}
App.MsgsController.addMsg(data.message);
});
socketio.on("own", function (data) {
App.MsgsController.addMsg(data.message);
});
App = Ember.Application.create();
App.Msg = Ember.Object.extend({
value: null
});
App.MsgsController = Ember.ArrayController.create({
content: [],
addMsg: function (value) {
var msg = App.Msg.create({
value: value
});
this.pushObject(msg);
}
});
App.TextField = Ember.TextField.extend({
insertNewline: function() {
this.get("controller").send("sendMsg");
}
});
App.ApplicationController = Ember.Controller.extend({
currentMsg: 't',
sendMsg: function () {
var currentMsg = this.get('currentMsg');
if(currentMsg) {
socketio.emit("message", { message: currentMsg, id: id});
this.set('currentMsg', '');
}
}
});
App.ApplicationController.sendMsg 呼び出しの後に App.TextField にフォーカスしたいと思います。
私は試した
App.TextField.$().focus()
しかし、そのメソッド内でのみ $() を使用できるようです。
誰か助けてくれませんか?
編集:わかりました、答えが見つかりました。
App.TextField は「クラス」のようなもので、ビュー上のものはインスタンスです。
ビューに ID を追加する必要があります
{{#view App.TextField valueBinding="currentMsg" placeholder="your message" id="mytextfield"}}{{/view}}
jqueryセレクターを使用してインスタンスにアクセスします
$('#mytextfield').focus();