PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/_posts/2013-03-28-backbone-tutorial-15.md

http://github.com/alexyoung/dailyjs
Markdown | 120 lines | 93 code | 27 blank | 0 comment | 0 complexity | c70fb44098db82bb6b4cfaca1669ac6e MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. ---
  2. layout: post
  3. title: "Backbone.js Tutorial: Updates for 1.0, Clear Complete"
  4. author: Alex Young
  5. categories:
  6. - backbone.js
  7. - mvc
  8. - node
  9. - backgoog
  10. - bootstrap
  11. ---
  12. <ul class="parts">
  13. <li><a href="http://dailyjs.com/2012/11/29/backbone-tutorial-1/">Part 1: Build Environment</a></li>
  14. <li><a href="http://dailyjs.com/2012/12/06/backbone-tutorial-2/">Part 2: Google's APIs and RequireJS</a></li>
  15. <li><a href="http://dailyjs.com/2012/12/13/backbone-tutorial-3/">Part 3: Authenticating with OAuth2</a></li>
  16. <li><a href="http://dailyjs.com/2012/12/20/backbone-tutorial-4/">Part 4: Backbone.sync</a></li>
  17. <li><a href="http://dailyjs.com/2012/12/27/backbone-tutorial-5/">Part 5: List Views</a></li>
  18. <li><a href="http://dailyjs.com/2013/01/03/backbone-tutorial-6/">Part 6: Creating Lists</a></li>
  19. <li><a href="http://dailyjs.com/2013/01/10/backbone-tutorial-7/">Part 7: Editing Lists</a></li>
  20. <li><a href="http://dailyjs.com/2013/01/17/backbone-tutorial-8/">Part 8: Deleting Lists</a></li>
  21. <li><a href="http://dailyjs.com/2013/01/24/backbone-tutorial-9/">Part 9: Tasks</a></li>
  22. <li><a href="http://dailyjs.com/2013/01/31/backbone-tutorial-10/">Part 10: Oh No Not More Tasks</a></li>
  23. <li><a href="http://dailyjs.com/2013/02/07/backbone-tutorial-11/">Part 11: Spies, Stubs, and Mocks</a></li>
  24. <li><a href="http://dailyjs.com/2013/02/14/backbone-tutorial-12/">Part 12: Testing with Mocks</a></li>
  25. <li><a href="http://dailyjs.com/2013/03/07/backbone-tutorial-13/">Part 13: Routes</a></li>
  26. <li><a href="http://dailyjs.com/2013/03/14/backbone-tutorial-14/">Part 14: Customising the UI</a></li>
  27. <li><a href="http://dailyjs.com/2013/03/28/backbone-tutorial-15/"><strong>Part 15: Updates for 1.0, Clear Complete</strong></a></li>
  28. <li><a href="http://dailyjs.com/2013/04/04/backbone-tutorial-16/">Part 16: jQuery Plugins</a></li>
  29. </ul>
  30. ###Preparation
  31. Before starting this tutorial, you'll need the following:
  32. * [alexyoung / dailyjs-backbone-tutorial](https://github.com/alexyoung/dailyjs-backbone-tutorial) at commit `711c9f6`
  33. * The API key from part 2
  34. * The "Client ID" key from part 2
  35. * Update `app/js/config.js` with your keys (if you've checked out my source)
  36. To check out the source, run the following commands (or use a suitable Git GUI tool):
  37. {% highlight text %}
  38. git clone git@github.com:alexyoung/dailyjs-backbone-tutorial.git
  39. cd dailyjs-backbone-tutorial
  40. git reset --hard 711c9f6
  41. {% endhighlight %}
  42. ###Updating to Backbone 1.0
  43. I updated bTask to work with Backbone 1.0, which required two small changes. The first was a change to the behaviour of callbacks in `Backbone.sync` -- the internal call to the `success` callback now only needs one argument, which is the response data. I think I've mentioned that on DailyJS before, but you shouldn't need to worry about this in your own Backbone projects unless you've written a custom `Backbone.sync` implementation.
  44. The second change was the collection `add` events were firing when the views called `fetch`. I fixed this by passing `reset: true` to the `fetch` options. Details on this have been included in [Backbone's documentation](http://backbonejs.org/) under "Upgrading to 1.0":
  45. > If you want to smartly update the contents of a Collection, adding new models, removing missing ones, and merging those already present, you now call `set` (previously named "update"), a similar operation to calling `set` on a Model. This is now the default when you call `fetch` on a collection. To get the old behavior, pass `{reset: true}`.
  46. ###Adding "Clear Complete"
  47. When a task in Google Tasks is marked as done, it will appear with strike-through and hang around in the list until it is cleared or deleted. Most Google Tasks clients will have a button that says "Clear Complete", so I added one to bTask.
  48. I added a method called `clear` to the `Tasks` collection which calls the `.clear` method from the Google Tasks API (rather than going through `Backbone.sync`):
  49. {% highlight javascript %}
  50. define(['models/task'], function(Task) {
  51. var Tasks = Backbone.Collection.extend({
  52. model: Task,
  53. url: 'tasks',
  54. clear: function(tasklist, options) {
  55. var success = options.success || function() {}
  56. , request
  57. , self = this
  58. ;
  59. options.success = function() {
  60. self.remove(self.filter(function(task) {
  61. return task.get('status') === 'completed';
  62. }));
  63. success();
  64. };
  65. request = gapi.client.tasks.tasks.clear({ tasklist: tasklist });
  66. Backbone.gapiRequest(request, 'update', this, options);
  67. }
  68. });
  69. return Tasks;
  70. });
  71. {% endhighlight %}
  72. I also added a button (using Bootstrap's built-in icons) to `app/js/templates/app.html`, and added an event to `AppView` (in `app/js/views/app.js`):
  73. {% highlight javascript %}
  74. var AppView = Backbone.View.extend({
  75. // ...
  76. events: {
  77. 'click .clear-complete': 'clearComplete'
  78. },
  79. // ...
  80. clearComplete: function() {
  81. var list = bTask.views.activeListMenuItem.model;
  82. bTask.collections.tasks.clear(list.get('id'), { success: function() {
  83. // Show some kind of user feedback
  84. }});
  85. return false;
  86. }
  87. });
  88. {% endhighlight %}
  89. I had to change `app/js/views/lists/menuitem.js` to set the current collection in the `open` method to make this work.
  90. ###Summary
  91. Because I've been reviewing Backbone's evolution as it progressed to 1.0 for DailyJS, updating this project wasn't too much effort. In general the 1.0 release is backwards compatible, so you should definitely consider upgrading your own projects. Also, now bTask has 'Clear Complete', I feel like it does enough of the standard Google Tasks features for me to actually use it regularly.
  92. Remember that you can try it out for yourself at [todo.dailyjs.com](http://todo.dailyjs.com/).
  93. The full source for this tutorial can be found in [alexyoung / dailyjs-backbone-tutorial, commit 705bcb4](https://github.com/alexyoung/dailyjs-backbone-tutorial/commit/705bcb4cd27d1794e52291e3c2d20e72a3b56022).