|
| 1 | +angular.module('ngMeteor.directives', []); |
| 2 | + |
| 3 | +angular.module('ngMeteor.services', []) |
| 4 | + .service("MeteorCollections", [ |
| 5 | + function () { |
| 6 | + var self = this; |
| 7 | + self.collections = {}; |
| 8 | + self.getCollection = function (name) { |
| 9 | + if (self.collections[name]) { |
| 10 | + return self.collections[name]; |
| 11 | + } else { |
| 12 | + self.collections[name] = new Meteor.Collection(name); |
| 13 | + return self.collections[name]; |
| 14 | + } |
| 15 | + } |
| 16 | + return self; |
| 17 | + |
| 18 | + }]) |
| 19 | + .factory("$meteorObject", [ |
| 20 | + function () { |
| 21 | + function ObjectFactory(collection, objectvalue) { |
| 22 | + if (!objectvalue) { |
| 23 | + return; |
| 24 | + } |
| 25 | + objectvalue.save = function () { |
| 26 | + collection.update({ |
| 27 | + _id: this._id |
| 28 | + }, objectvalue); |
| 29 | + } |
| 30 | + objectvalue.remove = function () { |
| 31 | + collection.remove({ |
| 32 | + _id: this._id |
| 33 | + }); |
| 34 | + } |
| 35 | + return objectvalue; |
| 36 | + } |
| 37 | + |
| 38 | + return ObjectFactory; |
| 39 | + }]) |
| 40 | + .factory("$meteor", ["$rootScope", "MeteorCollections", "$meteorObject", |
| 41 | + function ($rootScope, MeteorCollections, $meteorObject) { |
| 42 | + |
| 43 | + $rootScope.apply = _.debounce(function () { |
| 44 | + try { |
| 45 | + $rootScope.$digest(); |
| 46 | + } catch (e) { |
| 47 | + setTimeout($rootScope.apply, 0); |
| 48 | + } |
| 49 | + }, 100); |
| 50 | + /** Removes AngularJS transient properties from Object tree */ |
| 51 | + var cleanupAngularObject = function (value) { |
| 52 | + if (value instanceof Array) { |
| 53 | + for (var i = 0; i < value.length; i++) { |
| 54 | + cleanupAngularObject(value[i]); |
| 55 | + } |
| 56 | + } |
| 57 | + else if (value instanceof Object) { |
| 58 | + for (property in value) { |
| 59 | + if (/^\$+/.test(property)) { |
| 60 | + delete value[property]; |
| 61 | + } |
| 62 | + else { |
| 63 | + cleanupAngularObject(value[property]); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + ; |
| 68 | + }; |
| 69 | + |
| 70 | + function CollectionFactory(collection) { |
| 71 | + var collection = MeteorCollections.getCollection(collection); |
| 72 | + var value = []; |
| 73 | + |
| 74 | + function Collection(value) { |
| 75 | + value = {}; |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + Collection.observe = function (cursor, array) { |
| 80 | + cursor.observe({ |
| 81 | + "addedAt": function (document, atIndex, before) { |
| 82 | + //console.log(document); |
| 83 | + |
| 84 | + if (!array) { |
| 85 | + value = new $meteorObject(collection, document); |
| 86 | + } |
| 87 | + if (array) { |
| 88 | + value[atIndex] = new $meteorObject(collection, document); |
| 89 | + } |
| 90 | + $rootScope.apply(); |
| 91 | + }, |
| 92 | + "changedAt": function (newDocument, oldDocument, atIndex) { |
| 93 | + |
| 94 | + value[atIndex] = new $meteorObject(collection, newDocument); |
| 95 | + $rootScope.apply(); |
| 96 | + }, |
| 97 | + "removedAt": function (oldDocument, atIndex) { |
| 98 | + |
| 99 | + value.splice(atIndex, 1); |
| 100 | + $rootScope.apply(); |
| 101 | + } |
| 102 | + }) |
| 103 | + } |
| 104 | + Collection.find = function (selector, options, callback) { |
| 105 | + value = this instanceof Collection ? this : []; |
| 106 | + this.observe(collection.find(selector, options), true); |
| 107 | + return value; |
| 108 | + } |
| 109 | + Collection.findOne = function (selector, options, callback) { |
| 110 | + value = this instanceof Collection ? this : {}; |
| 111 | + value = new $meteorObject(collection, collection.find(selector, options).fetch()[0]); |
| 112 | + this.observe(collection.find(selector, options), false); |
| 113 | + return value; |
| 114 | + } |
| 115 | + Collection.insert = function (values) { |
| 116 | + values = angular.copy(values); |
| 117 | + cleanupAngularObject(values); |
| 118 | + return collection.insert(values); |
| 119 | + } |
| 120 | + Collection.update = function (selector, updateValues) { |
| 121 | + updateValues = angular.copy(updateValues); |
| 122 | + cleanupAngularObject(updateValues); |
| 123 | + delete updateValues._id; |
| 124 | + return collection.update(selector, { |
| 125 | + $set: updateValues |
| 126 | + }); |
| 127 | + } |
| 128 | + Collection.remove = function (selector) { |
| 129 | + return collection.remove(selector); |
| 130 | + } |
| 131 | + return Collection; |
| 132 | + } |
| 133 | + |
| 134 | + return CollectionFactory; |
| 135 | + }]) |
| 136 | +; |
| 137 | + |
| 138 | +angular.module('ngMeteor.blade', []) |
| 139 | + .run(['$templateCache', '$rootScope', '$compile', function ($templateCache, $rootScope, $compile) { |
| 140 | + var key, render; |
| 141 | + var __hasProp = {}.hasOwnProperty; |
| 142 | + for (key in Template) { |
| 143 | + if (!__hasProp.call(Template, key)) |
| 144 | + continue; |
| 145 | + render = Template[key]; |
| 146 | + $templateCache.put("" + key + ".blade", render()); |
| 147 | + } |
| 148 | + |
| 149 | + return Meteor.startup(function () { |
| 150 | + Spark.finalize(document.body); |
| 151 | + $('body').html($compile(Template.body())($rootScope)); |
| 152 | + return $rootScope.$apply(); |
| 153 | + }); |
| 154 | + } |
| 155 | + ]); |
| 156 | + |
| 157 | +angular.module('ngMeteor', ['ngMeteor.blade', 'ngMeteor.services', 'ngMeteor.directives']); |
0 commit comments