Skip to content
This repository was archived by the owner on Oct 5, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/templates/ui/app/root/root.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
angular.module('app.root')
.controller('RootCtrl', RootCtrl);

RootCtrl.$inject = ['messageBoardService'];
RootCtrl.$inject = ['$state', 'messageBoardService'];

function RootCtrl(messageBoardService) {
function RootCtrl($state, messageBoardService) {
var ctrl = this;
angular.extend(ctrl, {
currentState: $state.current.name,
messageBoardService: messageBoardService
});
}
Expand Down
10 changes: 7 additions & 3 deletions app/templates/ui/app/root/root.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<!-- not using ui-sref here on purpose -->
<a class="navbar-brand" href="/"><h1 id="logo">Sample App</h1></a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="/">Home</a></li>
<li><a href="/search">Search</a></li>
<li><a href="/create">Create</a></li>
<li><a ui-sref="root">Home</a></li>
<!-- flush search params when clicking Search when already on Search -->
<li ng-if="ctrl.currentState === 'root.search'"><a href="/search">Search</a></li>
<!-- preserve state when navigating back to Search from elsewhere -->
<li ng-if="ctrl.currentState !== 'root.search'"><a ui-sref="root.search">Search</a></li>
<li><a ui-sref="root.create">Create</a></li>
</ul>
<ml-user mode="inline"></ml-user>
</div><!-- /.navbar-collapse -->
Expand Down
41 changes: 37 additions & 4 deletions app/templates/ui/app/search/search.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,54 @@
'use strict';

angular.module('app.search')
.factory('SearchModel', SearchModel)
.controller('SearchCtrl', SearchCtrl);

SearchCtrl.$inject = ['$scope', '$location', 'userService', 'MLSearchFactory'];
SearchModel.$inject = ['MLSearchFactory'];
SearchCtrl.$inject = ['$scope', '$location', 'userService', 'SearchModel'];

// inherit from MLSearchController
var superCtrl = MLSearchController.prototype;
SearchCtrl.prototype = Object.create(superCtrl);

function SearchCtrl($scope, $location, userService, searchFactory) {
var ctrl = this;
function SearchModel(searchFactory) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it is possible to create a more generic factory or service that keeps a store of models by name. In the ctrl you would do something like var searchModel = modelService.get('search');. The service returns an empty model initially, but because it is a object reference it will automatically preserve any update the ctrl makes to it. If it could work that way at least..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure but is there any thing else we want to keep state of atm or would this just be anticipating some changes?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More of a useful thing SE's could use to preserve state in other controllers as well. Not really necessary if difficult..

var mlSearch = searchFactory.newContext();
return {
mlSearch: mlSearch,
response: null
};
}

function SearchCtrl($scope, $location, userService, searchModel) {
var ctrl = this;
var mlSearch = searchModel.mlSearch;

superCtrl.constructor.call(ctrl, $scope, $location, mlSearch);

ctrl.init();
(function init() {
//do not inherit method on purpose due to if statement

// monitor URL params changes (forward/back, etc.)
ctrl.$scope.$on('$locationChangeSuccess', ctrl.locationChange.bind(ctrl));

// capture initial URL params in mlSearch and ctrl
if ( ctrl.parseExtraURLParams ) {
ctrl.parseExtraURLParams();
}

if (searchModel.response) {
ctrl.updateSearchResults(searchModel.response);
ctrl.updateURLParams();
} else {
ctrl.mlSearch.fromParams().then( ctrl._search.bind(ctrl) );
}
})();

ctrl.updateSearchResults = function updateSearchResults(data) {
superCtrl.updateSearchResults.apply(ctrl, arguments);
searchModel.response = data;
return this;
};

ctrl.setSnippet = function(type) {
mlSearch.setSnippet(type);
Expand Down