/lib/kranium-src/backboneintegration.js

https://github.com/krawaller/kranium · JavaScript · 163 lines · 122 code · 36 blank · 5 comment · 28 complexity · a7a148235c157b321323beb574fc94df MD5 · raw file

  1. (function(global){
  2. var collectionsByInst = {};
  3. try {
  4. var backboneInitiated = false;
  5. var initBackbone = K.initBackbone = function(){
  6. try {
  7. Ti.include('/kranium/lib/backbone/underscore.js');
  8. Ti.include('/kranium/lib/backbone/backbone.js');
  9. var eventSplitter = /^(\w+)\s*(.*)$/;
  10. _.extend(Backbone.View.prototype, Backbone.Events, {
  11. tagName: 'view',
  12. make: function(tagName, attributes, content){
  13. return K.create({ type: tagName, attr: attributes, content: content });
  14. },
  15. delegateEvents: function(events) {
  16. Ti.API.log('delegately', [events, this.events]);
  17. if (! (events || (events = this.events))) return;
  18. $(this.el).unbind();
  19. for (var key in events) {
  20. var methodName = events[key];
  21. var match = key.match(eventSplitter);
  22. var eventName = match[1],
  23. selector = match[2];
  24. var method = _.bind(this[methodName], this);
  25. Ti.API.log('bindly', [eventName, selector]);
  26. if (selector === '') {
  27. $(this.el).bind(eventName, method);
  28. } else {
  29. $(this.el).delegate(selector, eventName, method);
  30. }
  31. }
  32. }
  33. });
  34. } catch(e){
  35. Ti.API.error(e);
  36. }
  37. if(Backbone){
  38. backboneInitiated = true;
  39. }
  40. }
  41. var simpleTypes = ["activityindicator", "alertdialog", "animation", "annotation", "button", "buttonbar", "coverflowview", "dashboarditem", "dashboardview", "emaildialog", "imageview", "label", "mapview", "maskedimage", "navigationgroup", "optiondialog", "picker", "pickercolumn", "pickerrow", "popover", "progressbar", "scrollview", "scrollableview", "searchbar", "slider", "splitwindow", "switch", "tab", "tabgroup", "tabbedbar", "tableview", "tableviewrow", "tableviewsection", "textarea", "textfield", "toolbar", "view", "webview", "window"];
  42. global.BackboneView = View.extend({
  43. renderCollection: function(){
  44. var collection = this.getCollection();
  45. if(collection && collection.map){
  46. var data = collection.map(function(model){
  47. return (model.el = K.creators[model.type](K.extend({ _modelId: model.id, _modelCid: model.cid }, model.attributes)));
  48. });
  49. this.el.setData(data);
  50. }
  51. },
  52. renderModel: function(model) {
  53. var collection = this.getCollection(),
  54. opts = {},
  55. type,
  56. el,
  57. key;
  58. if(collection){
  59. type = model.type;
  60. el = model && model.el;
  61. } else {
  62. opts = this._baseOpts;
  63. type = this.use || 'label';
  64. el = model.el;
  65. }
  66. var isSimple = simpleTypes.indexOf(type) !== -1,
  67. recreate = !isSimple,
  68. changed = model.changedAttributes();
  69. if(el){
  70. /*for(key in changed){
  71. if(typeof el[key] === 'undefined'){
  72. recreate = true;
  73. }
  74. }*/
  75. if(!recreate){
  76. for(key in changed){
  77. el[key] = changed[key];
  78. }
  79. } else {
  80. var $el = K(el),
  81. $parent = $el.parent();
  82. $el.remove();
  83. $parent.append((model.el = K.creators[type](K.extend(opts, model.attributes))));
  84. }
  85. } else {
  86. model.el = K.creators[type](model.attributes);
  87. }
  88. collection && collection.sort();
  89. return this;
  90. },
  91. getCollection: function(){
  92. return collectionsByInst[this._inst];
  93. },
  94. template: function(o){
  95. return K.extend({}, this._props, o, { type: this._klass });
  96. },
  97. init: function(o){
  98. if(!backboneInitiated){
  99. initBackbone();
  100. }
  101. var collection = o.collection || this.collection;
  102. if(collection && typeof o._inst !== undefined){
  103. collectionsByInst[o._inst] = collection;
  104. }
  105. delete this.collection;
  106. delete o.collection;
  107. if(this.model && !collection){
  108. var opts = K.extend({ type: this.use || this.type }, o, this.model.attributes);
  109. delete opts.model;
  110. this._baseOpts = o;
  111. this.el = this.model.el = K.create(opts);
  112. } else {
  113. this._super(o);
  114. }
  115. if(this.model){
  116. this.model.bind('change', this.renderModel.bind(this));
  117. }
  118. if(collection){
  119. this.renderCollection(collection);
  120. collection.bind('refresh', this.renderCollection.once(this));
  121. collection.bind('change', this.renderModel.bind(this));
  122. collection.bind('add', this.renderCollection.bind(this));
  123. }
  124. }
  125. });
  126. } catch (e){ Ti.API.error(e); }
  127. })(this);