{"id":250,"date":"2015-04-20T22:43:47","date_gmt":"2015-04-20T22:43:47","guid":{"rendered":"https:\/\/www.tech.shinynewthings.com\/?p=250"},"modified":"2015-04-20T22:43:47","modified_gmt":"2015-04-20T22:43:47","slug":"angularjs-state-service","status":"publish","type":"post","link":"https:\/\/www.tech.shinynewthings.com\/?p=250","title":{"rendered":"AngularJS &#8211; State Service"},"content":{"rendered":"<p>&#8216;use strict&#8217;;<\/p>\n<p>\/**<br \/>\n * @ngdoc service<br \/>\n * @name interceptcms.stateService<br \/>\n * @description<br \/>\n * # stateService<br \/>\n * Factory in the interceptcms.<br \/>\n *\/<\/p>\n<p>function stateHistory($state) {<\/p>\n<pre><code>var history = [];\nvar idx = 0;\n\n\/\/ function loadPrevious(idx) {\n\/\/   $state.go(history[idx].state, history[idx].params);     \n\/\/ }\n\nfunction addState() {\n  history.push({state: $state.current, params: $state.params});\n  idx += 1;\n}\n\nreturn {\n\n    loadListView: function(model) {\n        for (var i = history.length - 1; i &gt;= 0; i--) {\n          if (history[i].params.id === undefined &amp;&amp; history[i].params.model === model) {\n            $state.go(history[i].state, history[i].params); \n          }\n        }   \n    },\n\n    loadRecordView: function(model, id) {\n        var params = {};\n        params.id = id.replace('\/', '');\n        params.model = model;\n        $state.go(\"\/:model\/:id\", params); \n        \/\/$state.transitionTo(\"\/:model\/:id\", params, { reload: true, inherit: true, notify: true });\n    },\n\n      \/\/ FORM SUBMISSION CREATES AN ADDITIONAL STATE\n    goBack: function (model, createEdit, id) {\n        \/\/loadListView($state.params.model);      \n        if (id) {\n          this.loadRecordView(model, id);\n        } \n    },\n\n    stateLoaded: function () {\n        addState();\n    },\n\n    listView: function(model, id) {\n        if (!id) {\n          this.loadListView(model);\n        } else {\n          this.loadRecordView(model, id);\n        }\n    }\n\n};\n<\/code><\/pre>\n<p>}<\/p>\n<p>angular.module(&#8216;interceptcms&#8217;)<br \/>\n  .factory(&#8216;stateService&#8217;, stateHistory);<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8216;use strict&#8217;; \/** * @ngdoc service * @name interceptcms.stateService * @description * # stateService * Factory in the interceptcms. *\/ function stateHistory($state) { var history = []; var idx = 0; \/\/ function loadPrevious(idx) { \/\/ $state.go(history[idx].state, history[idx].params); \/\/ } function addState() { history.push({state: $state.current, params: $state.params}); idx += 1; } return { loadListView: function(model) [&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-250","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\/250","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=250"}],"version-history":[{"count":1,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts\/250\/revisions"}],"predecessor-version":[{"id":251,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts\/250\/revisions\/251"}],"wp:attachment":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}