-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathusing_emberjs.ngdoc
More file actions
113 lines (83 loc) · 4.17 KB
/
Copy pathusing_emberjs.ngdoc
File metadata and controls
113 lines (83 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
@ngdoc overview
@name Use Ember JS
@description
# Use Ember JS
**Note: This doc file has been kindly contributed and is in need of a little editing. More info on using SocketStream with Ember can be found on our Google Group. Pull requests to improve this page would be appreciated.**
Socketstream already support Ember MVC in the core and it is really easy to integrate your ember MVC end point into SocketStream framework.
Following are steps to create an end-point with Ember view in SocketStream.
### Defining Ember js MVC client end-point
Use `ss.client.define` to define the Ember MVC client end point in app.js
<pre>
ss.client.define('todos', {
view: './todos/todos.html',
css : ['libs', './todos/todos.css'],
code: ['libs', './todos'],
tmpl: []
});
</pre>
Add route for the newly defined end-point.
<pre>
ss.http.route('/todos', function(req, res) {
res.serveClient('todos');
}
</pre>
Enable Ember template engine in the app.js configuration.
<pre>
ss.client.templateEngine.use('ember');
// or use('ember', './todos') to limit ember only to 'client/todos' directory.
ss.client.templateEngine.use('ember', './todos');
</pre>
### Add client html views with handlebar templates
You can copy the Todos example html file from Ember website into `client/todos/todos.html`.
Note you need to add `<SocketStream>` in the head so socketstream framework gets loaded.
In the client todos.html, html view elements are described using handlebar scripts, not html tags directly.
The data model of the view and view event handlers are defined in the client app.js at `client/todos/app.js`.
For example, in todos.html, list of todo items are placed into a ul list view with data come from an ember array controller.
<pre>
// display createTodoView in todos.html
{{ view Todos.CreateTodoView id="new-todo" placeholder="What needs to be done?"}}
// list all todo items in ul view with data from ember array controller.
{{ #collection tagName="ul" contentBinding="Todos.todosController" itemClassBinding="content.isDone"}}
{{view Em.Checkbox titleBinding="content.title" valueBinding="content.isDone"}}
{{/collection}}
</pre>
In client app.js, we create an ember array controller to hold todo data model, as well as other views used in the html.
<pre>
window.Todos = Todos = Ember.Application.create({ ... });
// define Ember array controller that holds all todo items data model.
Todos.todosController = Em.ArrayController.create({
clearCompletedTodos: function() {
this.filterProperty('isDone', true).forEach(this.removeObject, this);
}
allAreDone: function(key, value) {
if (value !== undefined) {
this.setEach('isDone', value);
return value;
} else {
return !!this.get('length') && this.everyProperty('isDone', true);
}
}.property('@each.isDone')
}
// define new todo item view and view event handler.
Todos.CreateTodoView = Em.TextField.extend({
insertNewLine: function() {
Todos.todosController.createTodo(this.get('value');
}
});
</pre>
### Wiring up everything
First, make sure all templates declared in the html have been defined properly in your client app.js file.
Second, we need ember.js lib to be downloaded to the client before the html file gets parsed.
In client/code/libs, ensure lib loading order: 1.jquery, 2.ember.min.js, 3. ...
Finally, we need to load the client app.js that creates ember app and controller as early as possible so views are defined before DOM elements are created.
In client/code/ember/entry.js
<pre>
$(document).ready(function() {
require('/app');
}
</pre>
That's it, as simple as this.
Client Html file contains handlebar templates gets downloaded to browser, rendered by Ember, and all of the view variables and event handlers bound properly.
Now client ember MVC works seamlessly within socketstream framework.
In client app.js, you can use `ss.rpc()` to retrieve data from backend as usual.
You can also add client routing libraries such as Path.js, crossroads.js to give more capabilities to your MVC client.