PageRenderTime 24ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/Src/WS.EKA.Portal/Scripts/models/myCollect.js

http://mobileshop.codeplex.com
JavaScript | 107 lines | 96 code | 11 blank | 0 comment | 7 complexity | bc27d9c1739533c5bfde999f2fa69185 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. window.MyCollect = Backbone.Model.extend({
  2. urlRoot: 'api/collect/',
  3. url: function () {
  4. return this.urlRoot;
  5. }
  6. });
  7. window.MyCollectList = Backbone.Collection.extend({
  8. model: MyCollect,
  9. urlRoot: 'api/collect/',
  10. url: function () {
  11. return this.urlRoot + pageView.getCookieValue('uid');
  12. }
  13. });
  14. window.MyCollectView = Backbone.View.extend({
  15. el: '#jqt',
  16. initialize: function () {
  17. this.template = $('#MyCollectTemplate').html();
  18. this.collection = new MyCollectList();
  19. },
  20. events: {
  21. 'click .cancelFavorite': 'cancelCollect',
  22. 'click .collectlist .detail': 'renderDetail'
  23. },
  24. render: function () {
  25. var partial = { header: $('#HeaderTemplate').html(), footer: $('#FooterTemplate').html(),
  26. innerFooter: $('#InnerFooterTemplate').html()
  27. };
  28. var data = { btnListR: [{ name: 'avatar' }, { name: 'home'}] };
  29. this.$el.append(Mustache.render(this.template, data, partial));
  30. this.fetchCollects();
  31. return this;
  32. },
  33. renderDetail: function (e) {
  34. var target = $(e.currentTarget),
  35. productid = target.attr('productid'),
  36. price = target.closest('ul').find('.price').text();
  37. localStorage.setItem('productid', productid);
  38. localStorage.setItem('productDetail', JSON.stringify({ productid: productid, price: price }));
  39. pageView.goTo('ProductDetail');
  40. },
  41. fetchCollects: function () {
  42. var that = this,
  43. container = $('.collect');
  44. that.collection.fetch({
  45. success: function () {
  46. var template;
  47. if (that.collection.length == 0) {
  48. template = $('#EmptyCollectTemplate').html();
  49. container.html(Mustache.render(template, [], []));
  50. $('.footer').hide();
  51. return;
  52. } else {
  53. template = $('#CollectListTemplate').html();
  54. var data = { collects: that.collection.toJSON() };
  55. container.html(Mustache.render(template, data, []));
  56. }
  57. pageView.resizeScroll();
  58. }
  59. });
  60. },
  61. cancelCollect: function (e) {
  62. var that = this;
  63. if (confirm('????????')) {
  64. var target = $(e.currentTarget),
  65. productid = target.closest('ul').attr('collectid');
  66. MyCollectView.deleteFromCollect(productid, function () {
  67. that.fetchCollects();
  68. });
  69. }
  70. }
  71. }, {
  72. addToMyCollect: function (callback) {
  73. var collect = new MyCollect({
  74. productGuid: localStorage.getItem('productid')
  75. });
  76. collect.save('', '', {
  77. success: function () {
  78. callback.call();
  79. }
  80. });
  81. },
  82. deleteFromCollect: function (collectid, callback) {
  83. var collect = new MyCollect({
  84. id: 1,
  85. CollectId: collectid
  86. });
  87. collect.url = collect.urlRoot + collectid;
  88. collect.destroy({
  89. success: function (model, res) {
  90. if (res && res == 200) {
  91. callback.call();
  92. }
  93. }
  94. });
  95. }
  96. });