jQuery.namespace('Meatazine.popup');
(function (ns) {
  ns.SavedBooks = Backbone.View.extend({
    events: {
      "shown": "shownHandler",
      "click .well li": "item_clickHandler",
      "click .load": "loadButton_clickHandler",
      "click .autosave": "loadAutosaveButton_clickHandler",
      "click .disabled": "disabled_clickHandler",
    },
    initialize: function () {
      this.setElement(this.el);
      this.template = this.$('ul.well').find('script').html();
      this.registerModelListener();
      this.render();
    },
    render: function () {
      this.$('ul.well').empty();
      this.$('#books-local').html(Meatazine.utils.render(this.template, this.model.local.toJSON()));
      this.$('#books-cloud').html(Meatazine.utils.render(this.template, this.model.remote.toJSON()));
    },
    disabled_clickHandler: function (event) {
      event.stopPropagation();
      return false;
    },
    registerModelListener: function () {
      var local = this.model.local,
          remote = this.model.remote;
      local.on('add', this.local_addHandler, this);
      local.on('change', this.local_changeHandler, this);
      remote.on('add', this.remote_addHandler, this);
      remote.on('change', this.remote_changeHandler, this);
      remote.on('reset', this.remote_resetHandler, this);
    },
    item_clickHandler: function (event) {
      var target = $(event.currentTarget);
      if (target.hasClass('empty')) {
        return;
      }
      target.addClass('active')
        .siblings('.active').removeClass('active');
      this.$('.load').prop('disabled', false);
    },
    loadAutosaveButton_clickHandler: function () {
      M.book.load('bookauto');
      this.$el.modal('hide');
    },
    loadButton_clickHandler: function () {
      var isLogin = this.model.get('isLogin'),
          ul = isLogin ? '#books-cloud' : '#books-local',
          prefix = isLogin ? 'remote' : 'book',
          id = this.$(ul + ' .active').attr('data-item'),
          key = prefix + id,
          data = null;
      if (localStorage.getItem(key)) {
        M.book.load(key, id);
        this.$el.modal('hide');
      } else {
        data = {
          bookid: id, 
          openid: this.model.get('openid')
        };
        Meatazine.service.ServerCall.call('load', data, function (content) {
          localStorage.setItem(key, content);
          M.book.load(key, id);
          this.$el.modal('hide');
        }, null, this);
      }
    },
    local_addHandler: function (model, collection) {
      var item = Meatazine.utils.render(this.template, model.toJSON());
      this.$('#books-local').append(item);
    },
    local_changeHandler: function (model, options) {
      var index = this.model.remote.indexOf(model),
          item = Meatazine.utils.render(this.template, model.toJSON());
      this.$('#books-local').children().eq(index).replaceWith(item);
    },
    remote_addHandler: function (model, collection) {
      var item = Meatazine.utils.render(this.template, model.toJSON());
      this.$('#books-cloud').append(item);
    },
    remote_changeHandler: function (model, options) {
      var index = this.model.remote.indexOf(model),
          item = Meatazine.utils.render(this.template, model.toJSON());
      this.$('#books-cloud').children().eq(index).replaceWith(item);
    },
    remote_resetHandler: function (collection) {
      this.$('#books-cloud').html(Meatazine.utils.render(this.template, collection.toJSON()));
    },
    shownHandler: function (event) {
      this.$('.load').prop('disabled', true);
      this.$('.autosave').prop('disabled', !this.model.get('hasAutoSave'));
      this.$('.active').removeClass('active');
      
      var isLogin = this.model.get('isLogin');
      this.$('[href=#books-local]').toggleClass('disabled', isLogin).parent().toggleClass('active', !isLogin);
      this.$('[href=#books-cloud]').toggleClass('disabled', !isLogin).parent().toggleClass('active', isLogin);
      this.$('#books-local').toggleClass('active', !isLogin);
      this.$('#books-cloud').toggleClass('active', isLogin);
    },
  });
})(Meatazine.popup);


@@ -1744,32 +1744,37 @@
 dler: function (
+event
 ) %7B%0A      var is
@@ -1763,38 +1763,47 @@
 nt) %7B%0A      
-var isLogin = 
+// %E5%8A%A0%E8%BD%BD%E8%BF%9C%E7%A8%8B%E4%BF%9D%E5%AD%98%E7%9A%84%E6%9D%82%E5%BF%97%0A      if (
 this.model.g
@@ -1815,27 +1815,28 @@
 sLogin')
-,%0A 
+) %7B%0A
         
  ul = is
@@ -1831,133 +1831,37 @@
     
- ul = isLogin ? '#books-cloud' : '#books-local',%0A          prefix = isLogin ? 'remote' : 'book',%0A          id = this.$(ul + '
+var id = this.$('#books-cloud
  .ac
@@ -1900,27 +1900,33 @@
     
+  
 key = 
-prefix
+'remote'
  + id,%0A
+  
     
@@ -1942,61 +1942,24 @@
 a = 
-null;
+%7B
 %0A      
-if (localStorage.getItem(key)) %7B%0A
         
 M.bo
@@ -1958,117 +1958,24 @@
     
-M.
 book
-.load(key, id);%0A        this.$el.modal('hide');%0A      %7D else %7B%0A        data = %7B%0A          bookid: id, %0A
+id: id,%0A    
     
@@ -2013,16 +2013,20 @@
 penid')%0A
+    
         
@@ -2243,25 +2243,219 @@
 his);%0A      
-%7D
+  return;%0A      %7D%0A      %0A      // %E5%8A%A0%E8%BD%BD%E6%9C%AC%E5%9C%B0%E4%BF%9D%E5%AD%98%E7%9A%84%E6%9D%82%E5%BF%97%0A      var id = this.$('#books-local .active').attr('data-item'),%0A          key = 'book' + id;%0A      M.book.load(key, id);%0A      this.$el.modal('hide');
 %0A    %7D,%0A    

重构读取机制，有远端的都读远端