PageRenderTime 91ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/Resources/alloy.js

https://bitbucket.org/pedrobrasileiro/testealloy
JavaScript | 137 lines | 119 code | 18 blank | 0 comment | 26 complexity | 51bae1e435afa22e0a698f8e8fe2f908 MD5 | raw file
Possible License(s): Apache-2.0
  1. function ucfirst(text) {
  2. return text ? text[0].toUpperCase() + text.substr(1) : text;
  3. }
  4. function isTabletFallback() {
  5. return !(Math.min(Ti.Platform.displayCaps.platformHeight, Ti.Platform.displayCaps.platformWidth) < 700);
  6. }
  7. var _ = require("alloy/underscore")._, Backbone = require("alloy/backbone");
  8. exports._ = _;
  9. exports.Backbone = Backbone;
  10. exports.M = function(name, modelDesc, migrations) {
  11. var config = modelDesc.config, type = (config.adapter ? config.adapter.type : null) || "localDefault";
  12. type === "localDefault" && (type = "sql");
  13. var adapter = require("alloy/sync/" + type), extendObj = {
  14. defaults: config.defaults,
  15. sync: function(method, model, opts) {
  16. var config = model.config || {}, adapterObj = config.adapter || {}, type = (config.adapter ? config.adapter.type : null) || "localDefault";
  17. type === "localDefault" && (type = "sql");
  18. require("alloy/sync/" + type).sync(model, method, opts);
  19. }
  20. }, extendClass = {};
  21. migrations && (extendClass.migrations = migrations);
  22. _.isFunction(adapter.beforeModelCreate) && (config = adapter.beforeModelCreate(config) || config);
  23. var Model = Backbone.Model.extend(extendObj, extendClass);
  24. Model.prototype.config = config;
  25. _.isFunction(adapter.afterModelCreate) && adapter.afterModelCreate(Model);
  26. _.isFunction(modelDesc.extendModel) && (Model = modelDesc.extendModel(Model) || Model);
  27. return Model;
  28. };
  29. exports.C = function(name, modelDesc, model) {
  30. var extendObj = {
  31. model: model,
  32. sync: function(method, model, opts) {
  33. var config = model.config || {}, type = (config.adapter ? config.adapter.type : null) || "localDefault";
  34. type === "localDefault" && (type = "sql");
  35. require("alloy/sync/" + type).sync(model, method, opts);
  36. }
  37. }, Collection = Backbone.Collection.extend(extendObj), config = Collection.prototype.config = model.prototype.config, type = (config.adapter ? config.adapter.type : null) || "localDefault", adapter = require("alloy/sync/" + type);
  38. _.isFunction(adapter.afterCollectionCreate) && adapter.afterCollectionCreate(Collection);
  39. _.isFunction(modelDesc.extendModel) && (Collection = modelDesc.extendCollection(Collection) || Collection);
  40. return Collection;
  41. };
  42. exports.A = function(t, type, parent) {
  43. _.extend(t, Backbone.Events);
  44. (function() {
  45. var al = t.addEventListener, rl = t.removeEventListener, oo = t.on, of = t.off, tg = t.trigger, cbs = {}, ctx = _.extend({}, Backbone.Events);
  46. if (!al || !rl) return;
  47. t.trigger = function() {
  48. ctx.trigger.apply(ctx, Array.prototype.slice.apply(arguments));
  49. };
  50. t.on = function(e, cb, context) {
  51. var wcb = function(evt) {
  52. try {
  53. _.bind(tg, ctx, e, evt)();
  54. } catch (E) {
  55. Ti.API.error("Error triggering '" + e + "' event: " + E);
  56. }
  57. };
  58. if (!cbs[e]) {
  59. cbs[e] = {};
  60. al.call(t, e, wcb);
  61. }
  62. cbs[e][cb] = wcb;
  63. _.bind(oo, ctx, e, cb, context)();
  64. };
  65. t.off = function(e, cb, context) {
  66. var f = cbs[e] ? cbs[e][cb] : null;
  67. if (f) {
  68. _.bind(of, ctx, e, cb, context)();
  69. delete cbs[e][cb];
  70. if (cbs[e].length === 0) {
  71. delete cbs[e];
  72. rl.call(t, e, f);
  73. }
  74. f = null;
  75. }
  76. };
  77. })();
  78. return t;
  79. };
  80. exports.getWidget = function(id, name, args) {
  81. Ti.API.warn("Alloy.getWidget() is deprecated, use Alloy.createWidget() instead.");
  82. return exports.createWidget(id, name, args);
  83. };
  84. exports.createWidget = function(id, name, args) {
  85. return new (require("alloy/widgets/" + id + "/controllers/" + (name || "widget")))(args);
  86. };
  87. exports.getController = function(name, args) {
  88. Ti.API.warn("Alloy.getController() is deprecated, use Alloy.createController() instead.");
  89. return exports.createController(name, args);
  90. };
  91. exports.createController = function(name, args) {
  92. return new (require("alloy/controllers/" + name))(args);
  93. };
  94. exports.getModel = function(name, args) {
  95. Ti.API.warn("Alloy.getModel() is deprecated, use Alloy.createModel() instead.");
  96. return exports.createModel(name, args);
  97. };
  98. exports.createModel = function(name, args) {
  99. return new (require("alloy/models/" + ucfirst(name)).Model)(args);
  100. };
  101. exports.getCollection = function(name, args) {
  102. Ti.API.warn("Alloy.getCollection() is deprecated, use Alloy.createCollection() instead.");
  103. return exports.createCollection(name, args);
  104. };
  105. exports.createCollection = function(name, args) {
  106. return new (require("alloy/models/" + ucfirst(name)).Collection)(args);
  107. };
  108. exports.isTablet = function() {
  109. try {
  110. var psc = require("ti.physicalSizeCategory");
  111. return psc.physicalSizeCategory === "large" || psc.physicalSizeCategory === "xlarge";
  112. } catch (e) {
  113. Ti.API.warn("Could not find ti.physicalSizeCategory module, using fallback for Alloy.isTablet");
  114. return isTabletFallback();
  115. }
  116. return isTabletFallback();
  117. }();
  118. exports.isHandheld = !exports.isTablet;
  119. exports.version = "0.3.2";