javascript - $BROADCASTing from service to controller -
i have little concern here
this comes service named betslipfactory
removeslip: function(slip) { return betslipselectionrequest('/betslip/removeselection', { game: slip.game, pair: slip.pair, line: slip.line }); } then have function in controller service
$scope.removeslip = function(slip) { $rootscope.$broadcast('betslip:removelines', slip); betslipfactory.removeslip(slip) } next have controller in different scope named linesctrl , have function here calls couple functions service betslipfactory kind of toggle function
$rootscope.$on('betslip:removelines', function(event, slip) { if (slip) { betslipfactory.remove(line, row, type); }; }); $scope.addlinetobetslip = function(line, row, type) { var spreadselected = (row.spreadselected && type === 'spread'), totalselected = (row.totalselected && type === 'total'), moneylineselected = (row.moneylineselected && type === 'moneyline'); if (spreadselected || totalselected || moneylineselected) { betslipfactory.remove(line, row, type); }else { betslipfactory.add(line, row, type); } }; and html:
<button ng-click="removeslip(slip)"></button> and:
<td ng-class="!row.moneylineselected ? 'lines-hover' : 'line-selected'"> <a ng-click="addlinetobetslip(line, row, 'moneyline')"> <span ng-hide="row.nomoneyline">{{:: row.moneyline}}</span> </a> </td> what need: combine scopes, when function $scope.removeslip(slip) call, need call $scope.addlinetobetslip(line, row, type) , function should call betslipfactory.remove(line, row, type); within if statement.
when call $scope.removeslip(slip) need kill slip parameter, within scope of betslipfactory works great.
i recorded video see talking about, let me explain video little bit.
in first 2 tries might see able select , deselect , works great, in 3rd , 4th try, see select line, , go call , removeslip(slip) when play x on right, , in order deselect line on left have manually.
so started fiddle showing process dumbed way down compared plnkr started after. here using 2 separate controllers , service (factory) manage data. can done without using $rootscope or $broadcast. can take have done here , integrate code posted on plnkr. below can see quite simple process
html:
<div ng-app="testapp"> <div id="colleft" ng-controller="leftcontroller"> <div ng-repeat="bet in possiblebets"> <button ng-class="!bet.moneylineselected ? 'lines-hover' : 'line-selected'" ng-click="addlinetobetslip(bet)">{{bet.name}}</button> </div> </div> <div id="colright" ng-controller="rightcontroller"> bets:<br> <div ng-repeat="bet in bets"> active bet: {{bet.name}} - <button ng-click="removelinefrombetslip(bet)">×</button> </div> </div> </div> css:
.lines-hover { } .line-selected { background:yellow; } #colleft { width:65%; background:#f00; float:left; } #colright { width:35%; background:gray; float:left; } and js
var app = angular.module('testapp',[]); app.controller('leftcontroller', function($scope, betslipfactory) { // data data db $scope.possiblebets = [ {name:'bet 1',moneylineselected:false}, {name:'bet 2',moneylineselected:false}, {name:'bet 3',moneylineselected:false} ]; // think it, addlinetobetslip not name // since toggles bet $scope.addlinetobetslip = function(bet) { bet.moneylineselected = !bet.moneylineselected; // toggle moneylineselected boolean (bet.moneylineselected) ? betslipfactory.add(bet) : betslipfactory.remove(bet); // add or remove bet }; }); app.controller('rightcontroller', function($scope, betslipfactory) { $scope.bets = betslipfactory.getallbets(); // link active bets // remove bet factory $scope.removelinefrombetslip = function(bet) { bet.moneylineselected = false; betslipfactory.remove(bet); }; }); app.service('betslipfactory', function() { //a place keep active bets var thebets = []; return { add: function(bet) { // add bet local array thebets.push(bet); }, remove: function(bet) { // should error checking of index before removing var index = thebets.indexof(bet); thebets.splice(index,1); }, getallbets: function() { //simply return active bets return thebets; } } }); function log(msg) { console.log(msg); }
Comments
Post a Comment