i have little problem logic of app here. working $emit $emit not working want
i have 2 different controllers
betslipctrl.js
here have function
$scope.removeslip = function(slip) { //here emit $rootscope.$emit('betslip:removelines'); betslipfactory.removeslip(slip).then(function() { }, function(err) { console.log(err); }); };
and linesctrl.js
$scope.addlinetobetslip = function(line, row, type) { //here emit $rootscope.$on('betslip:removelines', function() { betslipfactory.remove(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); } };
i recorded video understand doing
as might see in video: in 1st attempt did, select 1 line , able remove no issue, in 2nd , 3rd attempt, selected 4 lines, , once try remove one, 4 lines has been removed. that's want avoid.
the object slip
1 need remove, line line , not whole lines.
update
see service here
remove: function(line, row, type) { var spreadselected = (row.spreadselected && type === 'spread'), totalselected = (row.totalselected && type === 'total'), moneylineselected = (row.moneylineselected && type === 'moneyline'), linevalue; if (spreadselected || totalselected || moneylineselected) { switch (type) { case 'spread': linevalue = row.spread.line; break; case 'total': linevalue = row.total.line; break; case 'moneyline': linevalue = row.moneylineid; break; default: break; } if (spreadselected) { row.spreadselected = false; } if (totalselected) { row.totalselected = false; } if (moneylineselected) { row.moneylineselected = false; } } return betslipselectionrequest('/betslip/removeselection', { game: row.game, pair: row.pair, line: linevalue }); }, add: function(line, row, type) { var spreadselected = (row.spreadselected && type === 'spread'), totalselected = (row.totalselected && type === 'total'), moneylineselected = (row.moneylineselected && type === 'moneyline'), linevalue; if (!(spreadselected || totalselected || moneylineselected)) { switch (type) { case 'spread': linevalue = row.spread.line; break; case 'total': linevalue = row.total.line; break; case 'moneyline': linevalue = row.moneylineid; break; default: break; } switch (type) { case 'spread': row.spreadselected = true; break; case 'total': row.totalselected = true; break; case 'moneyline': row.moneylineselected = true; break; } } return betslipselectionrequest('/betslip/addselection', { game: row.game, pair: row.pair, line: linevalue }); }, removeslip: function(slip) { return betslipselectionrequest('/betslip/removeselection', { game: slip.game, pair: slip.pair, line: slip.line }); },
the problem every time add new line, register handler generic event occurs whenever remove any line, not line added.
however, can add arguments when $broadcast
/$emit
ing events, as per documentation. suggest try following:
betslipctrl.js
$scope.removeslip = function(slip) { //here emit $rootscope.$emit('betslip:removelines', slip); betslipfactory.removeslip(slip).then(function() { }, function(err) { console.log(err); }); };
and linesctrl.js
$scope.addlinetobetslip = function(line, row, type) { //here emit // first argument $on() event object, // need define 2 params @ slip object passed in. $rootscope.$on('betslip:removelines', function(event, slip) { betslipfactory.removeslip(slip); }); 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); } };
i'm not sure got exact removal code right, points in right direction. might need make sure removing lines right slip first or something.
one final point: difference between $emit
, $broadcast
$emit
starts @ scope call on , goes up scope tree there, whereas $broadcast
starts @ scope call on , goes down scope tree. typically, if using $rootscope
, want use $broadcast
start @ top , allow other scopes in entire application listen event. way have done it, $rootscope
can have listeners on event, since there nothing higher it. might deliberate, seems mistake here, @ least put comment explaining why did way if deliberate.