Skip to content

Commit 4125ff0

Browse files
committed
Basic app to list created channels
Signed-off-by: Akash Manohar J <[email protected]>
1 parent 9a2a2e7 commit 4125ff0

File tree

14 files changed

+339
-5
lines changed

14 files changed

+339
-5
lines changed

app/assets/javascripts/application.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
//= require jquery
1414
//= require jquery_ujs
1515
//= require ember-dev
16-
//= require ember
16+
//= require lib/ember-rest.js
1717
//= require ember/kandan
1818
//= require_tree .
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Kandan.ChannelsController = Ember.ResourceController.create
2+
resourceType: Kandan.Channel
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Kandan.Channel extends Ember.Resource
2+
resourceUrl: '/channels'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Kandan.User extends Ember.Resource
2+
resourceUrl: '/users'
3+
4+
full_name: Ember.computed(
5+
()->
6+
"#{@get('first_name')} #{@get('last_name')}"
7+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div class='channel-list'>
2+
{{#each channels}}
3+
{{view Kandan.ShowChannelView channelBinding="this"}}
4+
{{/each}}
5+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{channel.id}}-{{channel.name}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Kandan.ListChannelsView = Ember.View.extend({
2+
templateName: 'ember/templates/channels/list'
3+
channelsBinding: 'Kandan.ChannelsController'
4+
5+
refreshList: ()->
6+
Kandan.ChannelsController.findAll()
7+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Kandan.ShowChannelView = Ember.View.extend({
2+
templateName: 'ember/templates/channels/show'
3+
className: ['channel-name']
4+
tagName: 'div'
5+
})
+280
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
/**
2+
Ember-REST.js
3+
4+
A simple library for RESTful resources in Ember.js
5+
6+
Copyright (c) 2012 Cerebris Corporation
7+
8+
Licensed under the MIT license:
9+
http://www.opensource.org/licenses/mit-license.php
10+
*/
11+
12+
/**
13+
A model class for RESTful resources
14+
15+
Extend this class and define the following properties:
16+
17+
* `resourceUrl` -- the base url of the resource (e.g. '/contacts');
18+
will append '/id' for individual resources (required)
19+
* `resourceName` -- the name used to contain the serialized data in this
20+
object's JSON representation (required only for serialization)
21+
* `resourceProperties` -- an array of property names to be returned in this
22+
object's JSON representation (required only for serialization)
23+
24+
Because `resourceName` and `resourceProperties` are only used for
25+
serialization, they aren't required for read-only resources.
26+
27+
You may also wish to override / define the following methods:
28+
29+
* `serialize()`
30+
* `serializeProperty(prop)`
31+
* `deserialize(json)`
32+
* `deserializeProperty(prop, value)`
33+
* `validate()`
34+
*/
35+
Ember.Resource = Ember.Object.extend({
36+
resourceUrl: Ember.required(),
37+
38+
/**
39+
Duplicate properties from another resource
40+
41+
* `source` -- an Ember.Resource object
42+
* `props` -- the array of properties to be duplicated;
43+
defaults to `resourceProperties`
44+
*/
45+
duplicateProperties: function(source, props) {
46+
var prop;
47+
48+
if (props === undefined) props = this.resourceProperties;
49+
50+
for(var i = 0; i < props.length; i++) {
51+
prop = props[i];
52+
this.set(prop, source.get(prop));
53+
}
54+
},
55+
56+
/**
57+
Generate this resource's JSON representation
58+
59+
Override this or `serializeProperty` to provide custom serialization
60+
61+
REQUIRED: `resourceProperties` and `resourceName` (see note above)
62+
*/
63+
serialize: function() {
64+
var name = this.resourceName,
65+
props = this.resourceProperties,
66+
prop,
67+
ret = {};
68+
69+
ret[name] = {};
70+
for(var i = 0; i < props.length; i++) {
71+
prop = props[i];
72+
ret[name][prop] = this.serializeProperty(prop);
73+
}
74+
return ret;
75+
},
76+
77+
/**
78+
Generate an individual property's JSON representation
79+
80+
Override to provide custom serialization
81+
*/
82+
serializeProperty: function(prop) {
83+
return this.get(prop);
84+
},
85+
86+
/**
87+
Set this resource's properties from JSON
88+
89+
Override this or `deserializeProperty` to provide custom deserialization
90+
*/
91+
deserialize: function(json) {
92+
Ember.beginPropertyChanges(this);
93+
for(var prop in json) {
94+
if (json.hasOwnProperty(prop)) this.deserializeProperty(prop, json[prop]);
95+
}
96+
Ember.endPropertyChanges(this);
97+
return this;
98+
},
99+
100+
/**
101+
Set an individual property from its value in JSON
102+
103+
Override to provide custom serialization
104+
*/
105+
deserializeProperty: function(prop, value) {
106+
this.set(prop, value);
107+
},
108+
109+
/**
110+
Load via ajax and deserialize
111+
112+
REQUIRED: `id`
113+
*/
114+
findResource: function() {
115+
var self = this;
116+
117+
return jQuery.ajax({
118+
url: this._resourceUrl(),
119+
dataType: 'json',
120+
type: 'GET'
121+
}).done( function(json) {
122+
self.deserialize(json);
123+
});
124+
},
125+
126+
/**
127+
Create (if new) or update (if existing) record via ajax
128+
129+
Will call validate() if defined for this record
130+
131+
If successful, updates this record's id and other properties
132+
by calling `deserialize()` with the data returned.
133+
134+
REQUIRED: `properties` and `name` (see note above)
135+
*/
136+
saveResource: function() {
137+
var self = this;
138+
139+
if (this.validate !== undefined) {
140+
var error = this.validate();
141+
if (error) {
142+
return {
143+
fail: function(f) { f(error); return this; },
144+
done: function() { return this; },
145+
always: function(f) { f(); return this; }
146+
};
147+
}
148+
}
149+
150+
return jQuery.ajax({
151+
url: this._resourceUrl(),
152+
data: this.serialize(),
153+
dataType: 'json',
154+
type: (this.isNew() ? 'POST' : 'PUT')
155+
}).done( function(json) {
156+
// Update properties
157+
if (json)
158+
self.deserialize(json);
159+
});
160+
},
161+
162+
/**
163+
Delete resource via ajax
164+
*/
165+
destroyResource: function() {
166+
return jQuery.ajax({
167+
url: this._resourceUrl(),
168+
dataType: 'json',
169+
type: 'DELETE'
170+
});
171+
},
172+
173+
/**
174+
Is this a new resource?
175+
*/
176+
isNew: function() {
177+
return (this._id() === undefined);
178+
},
179+
180+
/**
181+
@private
182+
183+
The URL for this resource, based on `resourceUrl` and `id` (which will be
184+
undefined for new resources).
185+
*/
186+
_resourceUrl: function() {
187+
var url = this.resourceUrl,
188+
id = this._id();
189+
190+
if (id !== undefined)
191+
url += '/' + id;
192+
193+
return url;
194+
},
195+
196+
/**
197+
@private
198+
199+
The id for this resource.
200+
*/
201+
_id: function() {
202+
return this.get('id');
203+
}
204+
});
205+
206+
/**
207+
A controller for RESTful resources
208+
209+
Extend this class and define the following:
210+
211+
* `resourceType` -- an Ember.Resource class; the class must have a `serialize()` method
212+
that returns a JSON representation of the object
213+
* `resourceUrl` -- (optional) the base url of the resource (e.g. '/contacts/active');
214+
will default to the `resourceUrl` for `resourceType`
215+
*/
216+
Ember.ResourceController = Ember.ArrayController.extend({
217+
resourceType: Ember.required(),
218+
219+
/**
220+
@private
221+
*/
222+
init: function() {
223+
this._super();
224+
this.clearAll();
225+
},
226+
227+
/**
228+
Create and load a single `Ember.Resource` from JSON
229+
*/
230+
load: function(json) {
231+
var resource = this.get('resourceType').create().deserialize(json);
232+
this.pushObject(resource);
233+
},
234+
235+
/**
236+
Create and load `Ember.Resource` objects from a JSON array
237+
*/
238+
loadAll: function(json) {
239+
for (var i=0; i < json.length; i++)
240+
this.load(json[i]);
241+
},
242+
243+
/**
244+
Clear this controller's contents (without deleting remote resources)
245+
*/
246+
clearAll: function() {
247+
this.set("content", []);
248+
},
249+
250+
/**
251+
Replace this controller's contents with an ajax call to `url`
252+
*/
253+
findAll: function() {
254+
var self = this;
255+
256+
return jQuery.ajax({
257+
url: this._resourceUrl(),
258+
dataType: 'json',
259+
type: 'GET'
260+
}).done( function(json) {
261+
self.clearAll();
262+
self.loadAll(json);
263+
});
264+
},
265+
266+
/**
267+
@private
268+
269+
Base URL for ajax calls
270+
271+
Will use the `resourceUrl` set for this controller, or if that's missing,
272+
the `resourceUrl` specified for `resourceType`.
273+
*/
274+
_resourceUrl: function() {
275+
if (this.resourceUrl === undefined)
276+
return this.get('resourceType').prototype.resourceUrl;
277+
else
278+
return this.resourceUrl;
279+
}
280+
});

app/controllers/main_controller.rb

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
class MainController < ApplicationController
2+
before_filter :authenticate_user!
3+
24
def index
5+
@channels = Channel.all
36
end
47
end

app/models/user.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class User < ActiveRecord::Base
22

33
has_many :activities
4+
before_save :ensure_authentication_token
45

56
# Include default devise modules. Others available are:
67
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable

app/views/layouts/application.html.erb

+11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@
99
<%- end %>
1010

1111
<%= javascript_include_tag "application" %>
12+
<%= javascript_tag do %>
13+
<%- if user_signed_in? %>
14+
<%- current_user_data = {
15+
:id => current_user.id,
16+
:name => "#{current_user.first_name} #{current_user.last_name}",
17+
:auth_token => current_user.authentication_token
18+
}
19+
%>
20+
$.data(document, 'current_user', <%= current_user_data.to_json.html_safe %>);
21+
<%- end %>
22+
<%- end %>
1223

1324
<%= csrf_meta_tags %>
1425
</head>

app/views/main/index.html.erb

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
1-
<h1>Main#index</h1>
2-
<p>Find me in app/views/main/index.html.erb</p>
1+
<div class="container">
2+
3+
<script type="text/x-handlebars">
4+
{{ view Kandan.ListChannelsView }}
5+
</script>
6+
7+
<%= javascript_tag do %>
8+
$(function() {
9+
Kandan.ChannelsController.loadAll(<%= @channels.to_json.html_safe %>);
10+
});
11+
<%- end %>
12+
13+
</div>

config/routes.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
Kandan::Application.routes.draw do
22

33
root :to => "main#index"
4-
5-
#devise_for :users
4+
devise_for :users
65

76
# The priority is based upon order of creation:
87
# first created -> highest priority.

0 commit comments

Comments
 (0)