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

/Server/RestfulObjects.Mvc.ClientApp/Scripts/spiro-models.js

http://restfulobjects.codeplex.com
JavaScript | 256 lines | 180 code | 63 blank | 13 comment | 17 complexity | b446586686b5f492c614d1e4e87db571 MD5 | raw file
  1. var roclient = (function () {
  2. var roc = {};
  3. roc.ArgumentMap = Backbone.Model.extend({
  4. }),
  5. // matches a action invoke resource 19.0 representation
  6. roc.Invoke = Backbone.Model.extend({
  7. getResult: function () {
  8. var result = this.get("result");
  9. var resultType = this.get("resultType");
  10. if (resultType === "scalar") {
  11. return result.value;
  12. }
  13. if (resultType === "object") {
  14. return new roc.DomainObject(result);
  15. }
  16. if (resultType === "list") {
  17. if (result) {
  18. return new roc.LinkList(result.value);
  19. }
  20. return null;
  21. }
  22. if (resultType === "void") {
  23. return null;
  24. }
  25. return null;
  26. },
  27. fetch: function (attributes, options) {
  28. if (this.method === "GET") {
  29. var map = JSON.stringify(this.toJSON());
  30. var encodedMap = encodeURI(map);
  31. this.url = this.url + "?" + encodedMap;
  32. Backbone.Model.prototype.fetch.call(this, attributes, options);
  33. }
  34. else if (this.method === "POST") {
  35. options = options || {};
  36. var parent = this;
  37. options.error = function (originalModel, resp, iOptions) {
  38. var rs = $.parseJSON(resp.responseText);
  39. //parent.set(rs);
  40. parent.trigger("requestFailed", { statusCode: resp.status, model: new roc.Invoke(rs) });
  41. };
  42. Backbone.Model.prototype.save.call(this, attributes, options);
  43. }
  44. else if (this.method === "PUT") {
  45. // set id so not new ?
  46. Backbone.Model.prototype.save.call(this, attributes, options);
  47. }
  48. }
  49. });
  50. // matches an action representation 18.0
  51. roc.ObjectAction = Backbone.Model.extend({
  52. getLinks: function () {
  53. return new roc.LinkList(this.get("links"));
  54. },
  55. getActionResult: function () {
  56. var links = this.getLinks();
  57. var resultsLink = _.find(links.models, function (i) {
  58. return i.targetType() === "urn:org.restfulobjects:repr-types/action-result";
  59. });
  60. return resultsLink.getTarget();
  61. },
  62. getHasArguments: function () {
  63. return this.get("extensions").hasParams;
  64. }
  65. });
  66. // matches a domain object representation 14.0
  67. roc.DomainObject = Backbone.Model.extend({
  68. url: function () {
  69. if (!this.selfUrl) {
  70. var links = this.get("links");
  71. var selfLink = _.find(links, function (i) {
  72. return i.rel === "self";
  73. });
  74. this.selfUrl = selfLink.href;
  75. }
  76. return this.selfUrl;
  77. },
  78. updateUrl: function () {
  79. var links = this.get("links");
  80. var updateLink = _.find(links, function (i) {
  81. return i.rel === "urn:org.restfulobjects:rels/update";
  82. });
  83. return updateLink.href;
  84. },
  85. getMemberLinks: function (id) {
  86. return new roc.LinkList(this.get("members")[id].links);
  87. },
  88. getObjectAction: function (id) {
  89. var links = this.getMemberLinks(id);
  90. var detailsLink = _.find(links.models, function (i) {
  91. return i.targetType() === "urn:org.restfulobjects:repr-types/object-action";
  92. });
  93. return detailsLink.getTarget();
  94. },
  95. getObjectActions: function () {
  96. var actions = [];
  97. for (id in this.get("members")) {
  98. var links = this.getMemberLinks(id);
  99. var detailsLink = _.find(links.models, function (i) {
  100. return i.targetType() === "urn:org.restfulobjects:repr-types/object-action";
  101. });
  102. actions.push(detailsLink.getTarget());
  103. }
  104. return actions;
  105. },
  106. getArgumentMap: function () {
  107. var map = new roc.ArgumentMap();
  108. map.url = this.updateUrl();
  109. map.id = this.get("instanceId");
  110. return map;
  111. },
  112. setEditMode: function (flag) {
  113. this.set("editMode", flag);
  114. },
  115. setToArgumentMap: function () {
  116. for (member in this.get("members")) {
  117. this.map.set(member, { "value": this.get("members")[member].value });
  118. }
  119. var parent = this;
  120. this.map.on("change", function () {
  121. parent.setFromArgumentMap();
  122. });
  123. },
  124. setFromArgumentMap: function () {
  125. this.set(this.map.attributes);
  126. },
  127. save: function (attributes, options) {
  128. this.map = this.getArgumentMap();
  129. this.setToArgumentMap();
  130. this.map.save(attributes, options);
  131. }
  132. });
  133. // matches the Link representation 2.7
  134. roc.Link = Backbone.Model.extend({
  135. // the url that this link points to
  136. targetUrl: function () {
  137. var href = this.get("href");
  138. return href;
  139. },
  140. // the media type that this link points to
  141. targetType: function () {
  142. var type = this.get("type");
  143. var parms = type.split(";");
  144. for (var i = 0; i < parms.length; i++) {
  145. if ($.trim(parms[i]).substring(0, 7) === "profile") {
  146. return parms[i].split("=")[1].replace(/\"/g, '');
  147. }
  148. }
  149. return "";
  150. },
  151. // the http method needed on the link
  152. method: function () {
  153. return this.get("method");
  154. },
  155. // get the object that this link points to
  156. getTarget: function () {
  157. var matchingType = RepTypeToModel[this.targetType()];
  158. var target = new matchingType();
  159. target.url = this.targetUrl();
  160. target.method = this.method();
  161. return target;
  162. }
  163. });
  164. // matches a list representation 11.0
  165. roc.LinkList = Backbone.Collection.extend({
  166. model: roc.Link,
  167. parse: function (response) {
  168. return response.value;
  169. }
  170. });
  171. // matches the home page representation 5.0
  172. roc.Home = Backbone.Model.extend({
  173. url: appPath,
  174. serviceLinksUrl: function () {
  175. var links = this.get("links");
  176. var services = _.find(links, function (o) { return o.rel.indexOf("urn:org.restfulobjects:rels/services") != -1; });
  177. return services.href;
  178. },
  179. getServiceLinks: function () {
  180. var services = new roc.LinkList();
  181. services.url = this.serviceLinksUrl();
  182. return services;
  183. }
  184. });
  185. // map of representation types to models
  186. var RepTypeToModel = {
  187. "urn:org.restfulobjects:repr-types/homepage": roc.Home,
  188. "urn:org.restfulobjects:repr-types/object": roc.DomainObject,
  189. "urn:org.restfulobjects:repr-types/object-action": roc.ObjectAction,
  190. "urn:org.restfulobjects:repr-types/action-result": roc.Invoke
  191. };
  192. return roc;
  193. } ());