PageRenderTime 79ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/browser/backbone-basics.js

https://github.com/joakin/backbone-basics
JavaScript | 147 lines | 80 code | 35 blank | 32 comment | 11 complexity | 5658e6445f42c2f809de1f215b79416c MD5 | raw file
  1. ;(function(e,t,n){function i(n,s){if(!t[n]){if(!e[n]){var o=typeof require=="function"&&require;if(!s&&o)return o(n,!0);if(r)return r(n,!0);throw new Error("Cannot find module '"+n+"'")}var u=t[n]={exports:{}};e[n][0].call(u.exports,function(t){var r=e[n][1][t];return i(r?r:t)},u,u.exports)}return t[n].exports}var r=typeof require=="function"&&require;for(var s=0;s<n.length;s++)i(n[s]);return i})({1:[function(require,module,exports){
  2. (function(){
  3. var Basics = require('./index')
  4. if (!window.Basics) {
  5. window.Basics = Basics
  6. } else {
  7. throw new Error('Basics global variable already defined')
  8. }
  9. })()
  10. },{"./index":2}],2:[function(require,module,exports){
  11. var Basics = {}
  12. Basics.Model = require('./model')
  13. Basics.Collection = require('./collection')
  14. Basics.View = require('./view')
  15. Basics.Fn = require('./fn')
  16. module.exports = Basics
  17. console.log('loaded!')
  18. },{"./model":3,"./collection":4,"./view":5,"./fn":6}],3:[function(require,module,exports){
  19. var mixin = require('./fn').mixin
  20. , _ = require('underscore')
  21. , Backbone = require('backbone')
  22. /*
  23. * Base model for the application.
  24. */
  25. // Provides helper for nested models.
  26. // By setting the property `types` on a model, you can specify which keys
  27. // correspond to complex types, let that be Models, Collections or custom
  28. // types. When parsed from json they will be instantiated with `new`, and
  29. // when asked toJSON they will be parsed by calling a `toJSON` method that
  30. // they have to implement.
  31. module.exports = Backbone.Model.extend({
  32. // This function takes a json object and parses its attributes
  33. // instantiating the ones that are present on the this.types attribute with
  34. // the type specified there.
  35. // This function is used on the default this.parse, but exposed just in
  36. // case you need to override parse and/or call it explicitly from
  37. // somewhere.
  38. // It also considers an option called unfold, to specify that it should
  39. // always create the submodels even if the value in the json is empty.
  40. _parseTypes: function(json, options) {
  41. if (this.types) _.each(this.types, function(Type, key) {
  42. if ((options.unfold || json[key]) && !(json[key] instanceof Type))
  43. json[key] = new Type(json[key], options)
  44. })
  45. return json
  46. },
  47. // Change the default parse function to parse types as well
  48. parse: function(json, options) {
  49. return this._parseTypes(json, options)
  50. },
  51. // This function takes an attributes object and converts its attributes
  52. // that are complex types into JSON by calling a `toJSON` attribute on the
  53. // object (must be implemented, otherwise fail loudly to be noted).
  54. // This function is used on the default this.toJSON, but exposed just in
  55. // case you need to override toJSON and/or call it explicitly from
  56. // somewhere.
  57. _typesToJSON: function(attrs) {
  58. if (this.types) _.each(this.types, function(Type, key) {
  59. attrs[key] = attrs[key] && attrs[key].toJSON()
  60. })
  61. return attrs
  62. },
  63. // Override the default toJSON object so that it converts typed attrs to js
  64. // objects.
  65. toJSON: function() {
  66. var attrs = _.clone(this.attributes)
  67. return this._typesToJSON(attrs)
  68. }
  69. }, {
  70. mixin: _.partial(mixin, this)
  71. })
  72. },{"underscore":7,"backbone":8,"./fn":6}],4:[function(require,module,exports){
  73. var mixin = require('./fn').mixin
  74. , _ = require('underscore')
  75. , Backbone = require('backbone')
  76. /*
  77. * Base Collection
  78. */
  79. module.exports = Backbone.Collection.extend({}, {
  80. mixin: _.partial(mixin, this)
  81. })
  82. },{"underscore":7,"backbone":8,"./fn":6}],6:[function(require,module,exports){
  83. var _ = require('underscore')
  84. var fn = {}
  85. fn.mixin = function(destiny, withMixin) {
  86. for (var key in withMixin) {
  87. if (withMixin.hasOwnProperty(key)) {
  88. var prop = withMixin[key]
  89. if (_.isObject(prop)) {
  90. prop = fn.mixin(_.isArray(prop)? []: {}, prop)
  91. }
  92. destiny[key] = prop
  93. }
  94. }
  95. return destiny
  96. }
  97. },{"underscore":7}],5:[function(require,module,exports){
  98. var mixin = require('./fn').mixin
  99. , _ = require('underscore')
  100. , Backbone = require('backbone')
  101. /*
  102. * Base View
  103. */
  104. module.exports = Backbone.View.extend({}, {
  105. mixin: _.partial(mixin, this)
  106. })
  107. },{"underscore":7,"backbone":8,"./fn":6}],7:[function(require,module,exports){
  108. module.exports = window._
  109. },{}],8:[function(require,module,exports){
  110. module.exports = window.Backbone
  111. },{}]},{},[1])
  112. ;