{"id":248,"date":"2015-04-20T22:43:18","date_gmt":"2015-04-20T22:43:18","guid":{"rendered":"https:\/\/www.tech.shinynewthings.com\/?p=248"},"modified":"2015-04-20T22:43:18","modified_gmt":"2015-04-20T22:43:18","slug":"angularjs-state-model-data-service","status":"publish","type":"post","link":"https:\/\/www.tech.shinynewthings.com\/?p=248","title":{"rendered":"AngularJS &#8211; State Model Data Service"},"content":{"rendered":"<p>&#8216;use strict&#8217;;<\/p>\n<p>\/**<br \/>\n * @ngdoc service<br \/>\n * @name interceptcms.config<br \/>\n * @description<br \/>\n * # config<br \/>\n * Factory in the interceptcms.<br \/>\n *\/<\/p>\n<p>var stateModelDataService = function ($state, dataService, modelService, RESOURCES) {<\/p>\n<pre><code>var setConfig = function(query, params) {\n    var config = {};\n    config.searchField = query.field || 'tag';\n    config.criteria = decodeURIComponent(params.id) || \"\";\n    config.skip = query.skip || 0;\n    config.limit = query.limit || RESOURCES.LIMIT;\n    config.dir = query.dir || 'asc';\n    config.sort = {};\n    config.query = {};\n    if (config.query.sort) {\n        config.sort[config.query.sort] = config.dir;\n    } else {\n        config.sort[primaryField] = config.dir;\n    }\n    return config;\n};\n\nvar isUpdate = function (array) {\n    return (array.indexOf(RESOURCES.EDIT) &gt; -1) ? true : false;\n};\n\nvar isCreate = function (array) {\n    return (array.indexOf(RESOURCES.CREATE) &gt; -1) ? true : false;\n};\n\nreturn function (callback, dataFiltered, setScopeValue) {\n\n    var urlArray = [];\n    \/\/ CONTROLLER VARIABLES\n    \/\/ DYNAMIC URL       \n    if ($state.params.model) {\n        urlArray.push($state.params.model);\n        if ($state.params.view) {\n            urlArray.push($state.params.view);\n        }\n        if ($state.params.id) {\n            urlArray.push($state.params.id);\n        }\n    } else {\n        \/\/ EXPLICIT URL\n        urlArray = $state.current.name.split('\/'); \n    }\n\n    \/\/ SET UP CONFIG\n    \/\/ DEFINE MODEL \/ ISCREATE \/ ISUPDATE \/ ID \/ BASEURL\n    var qs = window.location.href.split('?')[1];\n    var config = {};\n        config.model = {};\n        config.model._modelName = urlArray[0];\n        config.isCreate = isCreate(urlArray);\n        config.isUpdate = isUpdate(urlArray);\n        config.id = ($state.params.id) ? \"\/\" + $state.params.id : \"\";\n        config.baseUrl = RESOURCES.API_URL + urlArray[0];\n\n        config.search = ($state.params.search) ? \"\/search\/\" + $state.params.search : \"\";\n        config.qs = (qs) ? \"?\" + qs : \"\";\n        if (config.search === \"\") {\n            config.qs = config.qs.replace(\"&amp;dir=\", \" \");\n        }\n\n    \/\/ REQUEST AND RETURN MODEL DEFINITION\n    \/\/ PASSED TO CONTROLLER FUNCTION setScopeValue\n    if (config.model._modelName !== \"\") {\n        modelService.requestModel(config.model._modelName, setScopeValue);\n    }\n\n    \/\/ RETURN CONFIG TO CONTROLLER \/ CALLBACK\n    callback(config);\n\n    \/\/ RETURN DATA TO DATAFILTERED FOR OPTIONAL LOCAL FILTERING\n    if (!config.isCreate &amp;&amp; config.model._modelName !== \"\") {\n        dataService.getData(config.baseUrl + config.id + config.search + config.qs, config.model, dataFiltered);\n    }\n};\n<\/code><\/pre>\n<p>};<\/p>\n<p>angular.module(&#8216;interceptcms&#8217;)<br \/>\n  .factory(&#8216;stateModelDataService&#8217;, stateModelDataService);<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8216;use strict&#8217;; \/** * @ngdoc service * @name interceptcms.config * @description * # config * Factory in the interceptcms. *\/ var stateModelDataService = function ($state, dataService, modelService, RESOURCES) { var setConfig = function(query, params) { var config = {}; config.searchField = query.field || &#8216;tag&#8217;; config.criteria = decodeURIComponent(params.id) || &#8220;&#8221;; config.skip = query.skip || 0; config.limit [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-248","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts\/248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=248"}],"version-history":[{"count":1,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts\/248\/revisions"}],"predecessor-version":[{"id":249,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts\/248\/revisions\/249"}],"wp:attachment":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}