Skip to content

Commit f925e7d

Browse files
committed
Change Backbone.Todos app to use camelCase JSON services
1 parent 15a79cf commit f925e7d

File tree

3 files changed

+34
-29
lines changed

3 files changed

+34
-29
lines changed

src/Backbone.Todos/Global.asax.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public override object OnPost(Todo todo)
4141
return todo;
4242
}
4343

44+
public override object OnPut(Todo request)
45+
{
46+
return OnPost(request);
47+
}
48+
4449
public override object OnDelete(Todo request)
4550
{
4651
RedisManager.ExecAs<Todo>(r => r.DeleteById(request.Id));
@@ -56,6 +61,9 @@ public AppHost() : base("Backbone.js TODO", typeof(TodoService).Assembly) { }
5661

5762
public override void Configure(Funq.Container container)
5863
{
64+
//Set JSON web services to return idiomatic JSON camelCase properties
65+
ServiceStack.Text.JsConfig.EmitCamelCaseNames = true;
66+
5967
//Register Redis factory in Funq IOC
6068
container.Register<IRedisClientsManager>(new BasicRedisClientManager("localhost:6379"));
6169

src/Backbone.Todos/default.htm

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ <h1>Todos</h1>
6060
<!-- Templates -->
6161

6262
<script type="text/template" id="item-template">
63-
<div class="todo <%= Done ? 'Done' : '' %>">
63+
<div class="todo <%= done ? 'done' : '' %>">
6464
<div class="display">
65-
<input class="check" type="checkbox" <%= Done ? 'checked="checked"' : '' %> />
65+
<input class="check" type="checkbox" <%= done ? 'checked="checked"' : '' %> />
6666
<div class="todo-content"></div>
6767
<span class="todo-destroy"></span>
6868
</div>
@@ -73,24 +73,22 @@ <h1>Todos</h1>
7373
</script>
7474

7575
<script type="text/template" id="stats-template">
76-
<% if (Total) { %>
76+
<% if (total) { %>
7777
<span class="todo-count">
78-
<span class="number"><%= Remaining %></span>
79-
<span class="word"><%= Remaining == 1 ? 'item' : 'items' %></span> left.
78+
<span class="number"><%= remaining %></span>
79+
<span class="word"><%= remaining == 1 ? 'item' : 'items' %></span> left.
8080
</span>
8181
<% } %>
82-
<% if (Done) { %>
82+
<% if (done) { %>
8383
<span class="todo-clear">
8484
<a href="#">
85-
Clear <span class="number-done"><%= Done %></span>
86-
completed <span class="word-done"><%= Done == 1 ? 'item' : 'items' %></span>
85+
Clear <span class="number-done"><%= done %></span>
86+
completed <span class="word-done"><%= done == 1 ? 'item' : 'items' %></span>
8787
</a>
8888
</span>
8989
<% } %>
9090
</script>
91-
92-
93-
91+
9492
<script type="text/javascript">
9593
var _gaq = _gaq || [];
9694
_gaq.push(['_setAccount', 'UA-7722718-7']);

src/Backbone.Todos/todos.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@ $(function(){
1414

1515
// Default attributes for the todo.
1616
defaults: {
17-
Content: "empty todo...",
18-
Done: false
17+
content: "empty todo...",
18+
done: false
1919
},
2020

2121
// Ensure that each todo created has `content`.
2222
initialize: function() {
23-
if (!this.get("Content")) {
24-
this.set({"Content": this.defaults.Content});
23+
if (!this.get("content")) {
24+
this.set({"content": this.defaults.content});
2525
}
2626
},
2727

2828
// Toggle the `done` state of this todo item.
2929
toggle: function() {
30-
this.save({Done: !this.get("Done")});
30+
this.save({done: !this.get("done")});
3131
},
3232

3333
url : function() {
34-
return this.get("Id") ? 'todos/' + this.get("Id") : 'todos';
34+
return this.get("id") ? 'todos/' + this.get("id") : 'todos';
3535
},
3636

3737
// Remove this Todo from *localStorage* and delete its view.
@@ -58,7 +58,7 @@ $(function(){
5858

5959
// Filter down the list of all todo items that are finished.
6060
done: function() {
61-
return this.filter(function(todo){ return todo.get('Done'); });
61+
return this.filter(function(todo){ return todo.get('done'); });
6262
},
6363

6464
// Filter down the list to only todo items that are still not finished.
@@ -70,12 +70,12 @@ $(function(){
7070
// GUID in the database. This generates the next order number for new items.
7171
nextOrder: function() {
7272
if (!this.length) return 1;
73-
return this.last().get('Order') + 1;
73+
return this.last().get('order') + 1;
7474
},
7575

7676
// Todos are sorted by their original insertion order.
7777
comparator: function(todo) {
78-
return todo.get('Order');
78+
return todo.get('order');
7979
}
8080

8181
});
@@ -122,7 +122,7 @@ $(function(){
122122
// To avoid XSS (not that it would be harmful in this particular app),
123123
// we use `jQuery.text` to set the contents of the todo item.
124124
setContent: function() {
125-
var content = this.model.get('Content');
125+
var content = this.model.get('content');
126126
this.$('.todo-content').text(content);
127127
this.input = this.$('.todo-input');
128128
this.input.bind('blur', this.close);
@@ -142,7 +142,7 @@ $(function(){
142142

143143
// Close the `"editing"` mode, saving changes to the todo.
144144
close: function() {
145-
this.model.save({Content: this.input.val()});
145+
this.model.save({content: this.input.val()});
146146
$(this.el).removeClass("editing");
147147
},
148148

@@ -201,11 +201,10 @@ $(function(){
201201
// Re-rendering the App just means refreshing the statistics -- the rest
202202
// of the app doesn't change.
203203
render: function() {
204-
var done = Todos.done().length;
205204
this.$('#todo-stats').html(this.statsTemplate({
206-
Total: Todos.length,
207-
Done: Todos.done().length,
208-
Remaining: Todos.remaining().length
205+
total: Todos.length,
206+
done: Todos.done().length,
207+
remaining: Todos.remaining().length
209208
}));
210209
},
211210

@@ -224,9 +223,9 @@ $(function(){
224223
// Generate the attributes for a new Todo item.
225224
newAttributes: function() {
226225
return {
227-
Content: this.input.val(),
228-
Order: Todos.nextOrder(),
229-
Done: false
226+
content: this.input.val(),
227+
order: Todos.nextOrder(),
228+
done: false
230229
};
231230
},
232231

0 commit comments

Comments
 (0)