model/controler separation.
authorymh <ymh.work@gmail.com>
Mon, 09 Sep 2013 02:39:50 +0200
changeset 49 fcfa68f39f6b
parent 48 e2ccb0093452
child 50 2e52a8a10f8e
model/controler separation. Add controler for thesaurus.
src/p4l/static/p4l/js/p4l.js
src/p4l/templates/p4l/record_update_form.html
--- a/src/p4l/static/p4l/js/p4l.js	Mon Sep 09 01:48:26 2013 +0200
+++ b/src/p4l/static/p4l/js/p4l.js	Mon Sep 09 02:39:50 2013 +0200
@@ -19,31 +19,47 @@
             });
 });
 
-app.controller("RecordCtrl", function($scope, Api, context){
-    $scope.record = Api.record.get({recordId: context.record_id});
-    $scope.uriLabels = context.uri_labels;
+app.service("RecordModel", function(Api, context) {    
+    this.record = Api.record.get({recordId: context.record_id});
+    this.uriLabels = context.uri_labels;
+});
+
+app.controller("RecordCtrl", function($scope, RecordModel, context){
     
-    $scope.addSubject = function() {
-    	$scope.record.subjects.push($scope.addSubjectText);
-    	$scope.addSubjectText = '';
-    };
+    $scope.record = RecordModel.record;
+    $scope.uriLabels = RecordModel.uriLabels;
     
-    $scope.updateUriLabelDict = function(k,v) {
-    	$scope.uriLabels[k] = v;
-    };
         
     $scope.submitRecord = function() {
         $scope.record.$save({recordId: context.record_id});
     }
     
-    $scope.removeSubject = function(uri) {
-    	i = $scope.record.subjects.indexOf(uri);
+    $scope.removeFromList = function(uri, list) {
+    	i = list.indexOf(uri);
     	if(i>=0){
-    		$scope.record.subjects.splice(i, 1);
+    		list.splice(i, 1);
     	}
     };
 });
 
+app.controller("ThesaurusCtrl", function($scope, RecordModel) {
+
+    $scope.record = RecordModel.record;
+    $scope.uriLabels = RecordModel.uriLabels;
+    $scope.addSubjectText = '';
+    
+    $scope.addSubject = function() {
+        $scope.record.subjects.push($scope.addSubjectText);
+        $scope.addSubjectText = '';
+    };
+    
+    $scope.updateUriLabelDict = function(k,v) {
+        $scope.uriLabels[k] = v;
+    };
+    
+    
+});
+
 app.config(['$routeProvider', function($routeProvider) {
 //    $routeProvider.when('/', {controller: 'RecordCtrl', templateUrl: 'partials/record.html'});
 //    $routeProvider.otherwise({redirectTo: '/'});
--- a/src/p4l/templates/p4l/record_update_form.html	Mon Sep 09 01:48:26 2013 +0200
+++ b/src/p4l/templates/p4l/record_update_form.html	Mon Sep 09 02:39:50 2013 +0200
@@ -63,9 +63,9 @@
       <td>{{'subjects' | translate}}</td>
       <td>
           <ul ng-repeat="subject in record.subjects">
-              <li>{{uriLabels[subject]}} <small class="text-muted">({{subject}})</small> <a ng-click='removeSubject(subject)' class="btn btn-default btn-xs"><i class="glyphicon glyphicon-trash"></i></a></li>
+              <li>{{uriLabels[subject]}} <small class="text-muted">({{subject}})</small> <a ng-click='removeFromList(subject, record.subjects)' class="btn btn-default btn-xs"><i class="glyphicon glyphicon-trash"></i></a></li>
           </ul>
-          <form class="form-inline" role="form" ng-submit="addSubject()">
+          <form class="form-inline" role="form" ng-submit="addSubject()" ng-controller="ThesaurusCtrl">
 	        <div class="form-group col-md-8">
 	          <input id="id_addSubjectText" ng-model="addSubjectText" class="semantic-tree form-control col-md-10" type="text" placeholder="{{'Add a new subject' | translate}}" required="required"/>
 	          {{'or' | translate}}