PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/public/js/collection.js

https://github.com/u4xcat/bb-crud
JavaScript | 80 lines | 58 code | 13 blank | 9 comment | 1 complexity | 6b1506d9d0514a26db4732c29817a7c9 MD5 | raw file
  1. (function () {
  2. "use strict";
  3. console.log("Model & Collection Test");
  4. // ---
  5. var Memo = Backbone.Model.extend({
  6. idAttribute: "_id",
  7. defaults: {
  8. "content": ""
  9. },
  10. validate: function (attributes) {
  11. if (attributes.content === "") {
  12. return "content must be not empty.";
  13. }
  14. }
  15. });
  16. var MemoList = Backbone.Collection.extend({
  17. model: Memo,
  18. url: "/memo"
  19. });
  20. var memoList = new MemoList();
  21. console.log("Initial memoList.length: " + memoList.length);
  22. /**
  23. var memo = memoList.create({content: "Acro1"}, {
  24. success: function() {
  25. console.log("After create memoList: " + JSON.stringify(memoList));
  26. console.log("After create memoList.length: " + memoList.length);
  27. }
  28. });
  29. */
  30. var memo = new Memo({content: "Acro2"}, {collection: memoList});
  31. memo.save(null, {
  32. success: function () {
  33. console.log("After save memoList: " + JSON.stringify(memoList));
  34. console.log("After save memoList.length: " + memoList.length);
  35. }
  36. }).pipe(function () {
  37. return memoList.fetch({
  38. success: function () {
  39. console.log("After fetch memoList: " + JSON.stringify(memoList));
  40. console.log("After fetch memoList.length: " + memoList.length);
  41. }
  42. });
  43. }).pipe(function () {
  44. var tempMemo = memoList.find(function (item) {
  45. return item.get("content") === "Acro2";
  46. });
  47. return tempMemo.save({content: "Acro3"}, {
  48. success: function () {
  49. console.log("After save memoList: " + JSON.stringify(memoList));
  50. console.log("After save memoList.length: " + memoList.length);
  51. }
  52. });
  53. }).done(function () {
  54. var tempMemo = memoList.find(function (item) {
  55. return item.get("content") === "Acro3";
  56. });
  57. return tempMemo.destroy({
  58. success: function () {
  59. console.log("After destroy memoList: " + JSON.stringify(memoList));
  60. console.log("After destroy memoList.length: " + memoList.length);
  61. }
  62. });
  63. });
  64. memoList.add(memo);
  65. console.log("After add memo: " + JSON.stringify(memo));
  66. console.log("After add memoList.length: " + memoList.length);
  67. }());