PageRenderTime 39ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/backbone/backbone-tests.ts

https://github.com/singachea/DefinitelyTyped
TypeScript | 269 lines | 210 code | 54 blank | 5 comment | 2 complexity | 296c4140f8120aef8bed32104ad39f7e MD5 | raw file
Possible License(s): MIT
  1. /// <reference path="backbone.d.ts" />
  2. /// <reference path="../jquery/jquery.d.ts" />
  3. declare var _, $;
  4. function test_events() {
  5. var object = new Backbone.Events();
  6. object.on("alert", (msg) => alert("Triggered " + msg));
  7. object.trigger("alert", "an event");
  8. var onChange = () => alert('whatever');
  9. var context: any;
  10. object.off("change", onChange);
  11. object.off("change");
  12. object.off(null, onChange);
  13. object.off(null, null, context);
  14. object.off();
  15. }
  16. function test_models() {
  17. var Sidebar = Backbone.Model.extend({
  18. promptColor: function () {
  19. var cssColor = prompt("Please enter a CSS color:");
  20. this.set({ color: cssColor });
  21. }
  22. });
  23. var sidebar = new Sidebar();
  24. sidebar.on('change:color', (model, color) => $('#sidebar').css({ background: color }));
  25. sidebar.set({ color: 'white' });
  26. sidebar.promptColor();
  27. ////////
  28. var Note = Backbone.Model.extend({
  29. initialize: () => { },
  30. author: () => { },
  31. coordinates: () => { },
  32. allowedToEdit: (account) => {
  33. return true;
  34. }
  35. });
  36. var PrivateNote = Note.extend({
  37. allowedToEdit: function (account) {
  38. return account.owns(this);
  39. }
  40. });
  41. //////////
  42. var note = Backbone.Model.extend({
  43. set: function (attributes, options) {
  44. Backbone.Model.prototype.set.call(this, attributes, options);
  45. }
  46. });
  47. note.get("title")
  48. note.set({ title: "March 20", content: "In his eyes she eclipses..." });
  49. note.set("title", "A Scandal in Bohemia");
  50. }
  51. class Employee extends Backbone.Model {
  52. reports: EmployeeCollection;
  53. constructor (options? ) {
  54. super(options);
  55. this.reports = new EmployeeCollection();
  56. this.reports.url = '../api/employees/' + this.id + '/reports';
  57. }
  58. more() {
  59. this.reports.reset();
  60. }
  61. }
  62. class EmployeeCollection extends Backbone.Collection {
  63. findByName(key) { }
  64. }
  65. function test_collection() {
  66. var Book: Backbone.Model;
  67. var Library = Backbone.Collection.extend({
  68. model: Book
  69. });
  70. var Books: Backbone.Collection;
  71. Books.each(function (book) {
  72. });
  73. var titles = Books.map(function (book) {
  74. return book.get("title");
  75. });
  76. var publishedBooks = Books.filter(function (book) {
  77. return book.get("published") === true;
  78. });
  79. var alphabetical = Books.sortBy(function (book) {
  80. return null;
  81. });
  82. }
  83. //////////
  84. Backbone.history.start();
  85. module v1Changes {
  86. module events {
  87. function test_once() {
  88. var model = new Employee;
  89. model.once('invalid', () => { }, this);
  90. model.once('invalid', () => { });
  91. }
  92. function test_listenTo() {
  93. var model = new Employee;
  94. var view = new Backbone.View;
  95. view.listenTo(model, 'invalid', () => { });
  96. }
  97. function test_listenToOnce() {
  98. var model = new Employee;
  99. var view = new Backbone.View;
  100. view.listenToOnce(model, 'invalid', () => { });
  101. }
  102. function test_stopListening() {
  103. var model = new Employee;
  104. var view = new Backbone.View;
  105. view.stopListening(model, 'invalid', () => { });
  106. view.stopListening(model, 'invalid');
  107. view.stopListening(model);
  108. }
  109. }
  110. module modelandcollection {
  111. function test_url() {
  112. Employee.prototype.url = () => '/employees';
  113. EmployeeCollection.prototype.url = () => '/employees';
  114. }
  115. function test_parse() {
  116. var model = new Employee();
  117. model.parse('{}', {});
  118. var collection = new EmployeeCollection;
  119. collection.parse('{}', {});
  120. }
  121. function test_toJSON() {
  122. var model = new Employee();
  123. model.toJSON({});
  124. var collection = new EmployeeCollection;
  125. collection.toJSON({});
  126. }
  127. function test_sync() {
  128. var model = new Employee();
  129. model.sync();
  130. var collection = new EmployeeCollection;
  131. collection.sync();
  132. }
  133. }
  134. module model {
  135. function test_validationError() {
  136. var model = new Employee;
  137. if (model.validationError) {
  138. console.log('has validation errors');
  139. }
  140. }
  141. function test_fetch() {
  142. var model = new Employee({ id: 1 });
  143. model.fetch({
  144. success: () => { },
  145. error: () => { }
  146. });
  147. }
  148. function test_set() {
  149. var model = new Employee;
  150. model.set({ name: 'JoeDoe', age: 21 }, { validate: false });
  151. model.set('name', 'JoeDoes', { validate: false });
  152. }
  153. function test_destroy() {
  154. var model = new Employee;
  155. model.destroy({
  156. wait: true,
  157. success: (m?, response?, options?) => { },
  158. error: (m?, jqxhr?: JQueryXHR, options?) => { }
  159. });
  160. model.destroy({
  161. success: (m?, response?, options?) => { },
  162. error: (m?, jqxhr?: JQueryXHR) => { }
  163. });
  164. model.destroy({
  165. success: () => { },
  166. error: (m?, jqxhr?: JQueryXHR) => { }
  167. });
  168. }
  169. function test_save() {
  170. var model = new Employee;
  171. model.save({
  172. name: 'Joe Doe',
  173. age: 21
  174. },
  175. {
  176. wait: true,
  177. validate: false,
  178. success: (m?, response?, options?) => { },
  179. error: (m?, jqxhr?: JQueryXHR, options?) => { }
  180. });
  181. model.save({
  182. name: 'Joe Doe',
  183. age: 21
  184. },
  185. {
  186. success: () => { },
  187. error: (m?, jqxhr?: JQueryXHR) => { }
  188. });
  189. }
  190. function test_validate() {
  191. var model = new Employee;
  192. model.validate({ name: 'JoeDoe', age: 21 }, { validateAge: false })
  193. }
  194. }
  195. module collection {
  196. function test_fetch() {
  197. var collection = new EmployeeCollection;
  198. collection.fetch({ reset: true });
  199. }
  200. function test_create() {
  201. var collection = new EmployeeCollection;
  202. var model = new Employee;
  203. collection.create(model, {
  204. validate: false
  205. });
  206. }
  207. }
  208. module router {
  209. function test_navigate() {
  210. var router = new Backbone.Router;
  211. router.navigate('/employees', { trigger: true });
  212. router.navigate('/employees', true);
  213. }
  214. }
  215. }