PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/helper.js

https://github.com/watsoncj/backbone.associate
JavaScript | 71 lines | 57 code | 14 blank | 0 comment | 5 complexity | 9d1c6c821beee40408b2a18f7c2b69f1 MD5 | raw file
  1. (function() {
  2. var Fixtures, TestCollection, TestHelpers, TestModel, global, globals, module;
  3. var Backbone = this.Backbone,
  4. _ = this._;
  5. if (typeof window == 'undefined') {
  6. Backbone = require('backbone');
  7. _ = require('underscore');
  8. require('./helper');
  9. }
  10. TestModel = Backbone.Model.extend({
  11. defaults: {
  12. age: '',
  13. sex: '',
  14. name: ''
  15. },
  16. validate: function() {
  17. if ((this.attributes.valid !== null) && !this.attributes.valid) {
  18. return 'Invalid';
  19. }
  20. }
  21. });
  22. TestCollection = Backbone.Collection.extend({});
  23. Fixtures = {
  24. testModels: [
  25. { age: 35, sex: 'M', name: 'John' },
  26. { age: 26, sex: 'F', name: 'Joan' },
  27. { age: 24, sex: 'F', name: 'Jean' },
  28. { age: 33, sex: 'M', name: 'Joe' },
  29. { age: 26, sex: 'M', name: 'Jordan' }
  30. ],
  31. testCountry: {
  32. name: 'Canada',
  33. cities: [{ name: 'Calgary' }, { name: 'Regina' }]
  34. }
  35. };
  36. TestHelpers = {
  37. getRandomAttr: function(fixture, key) {
  38. var results = _.pluck(fixture, key);
  39. return results[_.random(0, results.length - 1)];
  40. },
  41. getExpectedResult: function(fixture, key, value) {
  42. var whereVal = {};
  43. whereVal[key] = value;
  44. return _.where(fixture, whereVal);
  45. }
  46. };
  47. globals = {
  48. TestModel: TestModel,
  49. TestCollection: TestCollection,
  50. Fixtures: Fixtures,
  51. TestHelpers: TestHelpers
  52. };
  53. for (global in globals) {
  54. module = globals[global];
  55. this[global] = module;
  56. }
  57. }).call(this);