PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/backbone/backbone-tests.ts

https://github.com/manafay/DefinitelyTyped
TypeScript | 113 lines | 79 code | 30 blank | 4 comment | 1 complexity | f266352abf82e851a442433675e8dc17 MD5 | raw file
Possible License(s): MIT
  1. /// <reference path="backbone.d.ts" />
  2. declare var _, $;
  3. function test_events() {
  4. var object = new Backbone.Events();
  5. object.on("alert", (msg) => alert("Triggered " + msg));
  6. object.trigger("alert", "an event");
  7. var onChange = () => alert('whatever');
  8. var context: any;
  9. object.off("change", onChange);
  10. object.off("change");
  11. object.off(null, onChange);
  12. object.off(null, null, context);
  13. object.off();
  14. }
  15. function test_models() {
  16. var Sidebar = Backbone.Model.extend({
  17. promptColor: function () {
  18. var cssColor = prompt("Please enter a CSS color:");
  19. this.set({ color: cssColor });
  20. }
  21. });
  22. var sidebar = new Sidebar();
  23. sidebar.on('change:color', (model, color) => $('#sidebar').css({ background: color }));
  24. sidebar.set({ color: 'white' });
  25. sidebar.promptColor();
  26. ////////
  27. var Note = Backbone.Model.extend({
  28. initialize: () => { },
  29. author: () => { },
  30. coordinates: () => { },
  31. allowedToEdit: (account) => {
  32. return true;
  33. }
  34. });
  35. var PrivateNote = Note.extend({
  36. allowedToEdit: function (account) {
  37. return account.owns(this);
  38. }
  39. });
  40. //////////
  41. var note = Backbone.Model.extend({
  42. set: function (attributes, options) {
  43. Backbone.Model.prototype.set.call(this, attributes, options);
  44. }
  45. });
  46. note.get("title")
  47. note.set({ title: "March 20", content: "In his eyes she eclipses..." });
  48. note.set("title", "A Scandal in Bohemia");
  49. }
  50. class Employee extends Backbone.Model {
  51. reports: EmployeeCollection;
  52. constructor (options? ) {
  53. super(options);
  54. this.reports = new EmployeeCollection();
  55. this.reports.url = '../api/employees/' + this.id + '/reports';
  56. }
  57. more() {
  58. this.reports.reset();
  59. }
  60. }
  61. class EmployeeCollection extends Backbone.Collection {
  62. url: string = "../api/employees";
  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. });
  81. }
  82. //////////
  83. Backbone.history.start();