Passing multiple data points to an action? VUEX - forms

So I have a form that collects 3 data points that looks like this:
<form class="" >
<table>
<tr>
<td>Maximum Temperature</td>
<td><input class="setSettings" v-model="settings.maxTMP" type="number" max="30" min="5"></td>
</tr>
<tr>
<td>Minimum Humidity</td>
<td><input class="setSettings" v-model="settings.minHUM" type="number" max="95" min="20"></td>
</tr>
<tr>
<td>Maximum CO2</td>
<td><input class="setSettings" v-model="settings.maxCO2" type="number" min="200" max="5000" step="10"></td>
</tr>
</table>
<button type="button" v-on:click="whatSettings(settings)">Update Settings</button>
</form>
My data in the script tag is stored in the components local data, not the store, ike this:
data: function() {
return {
settings: {
maxTMP: "",
minHUM: "",
maxCO2: ""
}
}
}
There's no reason to put it through a mutation and commit it to the store as the purpose for the action is merely to send the data via post request to receiving api.
My methods for the component look like this:
methods: {
...mapActions({
setSettings: 'setSettings'
}),
whatSettings(settings){
let foo = settings.maxTMP;
let bar = settings.minHUM;
let uhm = settings.maxCO2;
console.log(foo);
console.log(bar);
console.log(uhm);
this.setSettings(foo,bar,uhm)
}
},
And that action from the store is write like this:
setSettings(foo,bar,uhm) {
console.log("Set these settings");
console.log(foo);
console.log(bar);
console.log(uhm);
},
Please forgive all the console.log()'s. I have it like this cause I was trying to test out different combinations of things to figure out where it goes wrong. Right now when I click the Update Settings button the console prints the whatSettings() correctly so I know that foo, bar, uhm are the correct value as they are passed into the setSettings() action. The problem is in the action's logs. "Set these settings" and foo are printed correctly followed by a single undefined, not two. So I'm not sure exactly what's happening with bar & uhm. When I rearrange the order it's always the first that gets printed.
Is it a problem with multiple arguments being passed to the action? Ideally I would just like it to look like this directly in the button tag for neatness but that didn't work so I tried trouble shooting like this:
v-on:click="this.setSettings(settings.maxTMP, settings.minHUM, settings.maxCO2)"
Thanks for reading and I appreciate the help!

Well, after a little more looking around I found this and it fixed my problem: Axios post request with multiple parameters in vuex action
correct button tag:
<button type="button"
v-on:click="setSettings({
foo: settings.maxTMP,
bar: settings.minHUM,
uhm: settings.maxCO2
})"
>Update Settings</button>
correct vuex store action:
setSettings({commit}, {foo, bar, uhm}) {
console.log("Set these settings");
console.log(foo);
console.log(bar);
console.log(uhm);
},
Not entirely sure why the {commit} needs to be present, but it does otherwise I just get 3 undefined results in the log. But it works now!

Related

How to fix the red mark that appears when using binding.scala in intellij?

I am developing with scalajs and binding.scala. I'm using the IDE as an Intellij. However, when using dom macro in Intellij, the following red mark appears. this error appears when I use the attribute value of id in the input element as macro What is the solution?
This error(a.k.a. "cannot resolve symbol something") appears when you use the id attribute value of the input element as marco.
please see the link of image below.
this is my code image.
#dom
def render: xml.Elem = {
val name: _root_.com.thoughtworks.binding.Binding.Var[_root_.java.lang.String] = Var.apply("Binding.scala")
val show: _root_.com.thoughtworks.binding.Binding.Var[Boolean] = Var.apply(false)
<div>
<p>
<label for="showCheckbox">
<input type="checkbox" id="showCheckbox" onchange={e: Event => show.value = showCheckbox.value }/>
<span> Say hello to <input id="nameInput" value={name.value} oninput={_: Event => name.value = nameInput.value}/></span>
</label>
</p>
{
if (show.bind) {
<p>
Hello, {name.bind}!
</p>
} else {
<!-- Don't show hello. -->
}
}
</div>
}
I actually have the same problem. I have 2 ways dealing with it:
Ignore these exception - as they are only a problem within IntellIJ
(it compiles just fine).
Use for example JQuery like this:
import org.scalajs.jquery.jQuery
..
jQuery("#showCheckbox").value()
As soon as your id gets more dynamic - you will need something like that anyway (at least that is what I know;)) -> jQuery(s"#${elem.id}").value().
You could take advantage of the scalaJS Event passed in, maybe something like:
oninput={ev: Event => name.value = ev.target.asInstanceOf[HTMLInputElement].value}

How to delete objects on a Thymeleaf th:each iteration passing an ID to the controller?

My controller:
#GetMapping("/deletesocio/{id}")
public String delSocios(#PathVariable Long id){
socioSer.borrar(socioService.buscarPorId(id));
return "redirect:/webapp/socios";
}
My HTML
<tr th:each="soc : ${list}">
<td th:text="${soc.idSocio}">#</td>
<td th:text="${soc.nombreSocio}">Nombre</td>
<td class="button778"><button type="button"
th:href="#{/webapp/delsocio/${soc.idSocio}}"></button></td>
</tr>
I want to delete the object by clicking this button who pass de id to the controller (well, thats the idea), could any one help me please?? thank you very much
There are 2 parts to this...
1) The url expressions you should use is: #{/webapp/delsocio/{id}(id=${soc.idSocio})}
2) You can either make it a form with a submit button or style a regular link as a button as described here. Whichever solution you decide on will determine if you use th:action="#{/webapp/delsocio/{id}(id=${soc.idSocio})}" or th:href="#{/webapp/delsocio/{id}(id=${soc.idSocio})}".

Is there a difference between .add() in AngularJS vs. DOM?

Is there a difference between the .add() method in AngularJS and DOM? I'm using .add() in an AngularJS application and I'm running into *issues pushing data to a firebase. When I go to look at the documentation to do some research I found nothing in the AngularJS docs about this method. I found that .add() in DOM is for adding an option to a select (eg Object_of_Select.add(option, before);). But this doesn't really translate IMO to how I'm using it in AngularJS.
*The Issue I'm experiencing:
I have no problem with Firebase and persisting data, its super straightforward and awesome. I run into problems however, when I take an object (in this example "campaign") and push objects into a nested array of objects ("tactics"). The second I add local data, I can no longer "add" that data to the Firebase. No errors, just nothing.
So if anyone could point me in the right direction it would be super.
TEMPLATE
<!-- TITLE THE CAMPAIGN -->
<h3>{{campaign.name}}</h3>
<input type="text" placeholder="Campaign Title" ng-model="campaign.name"/>
<!-- LOOP THROUGH TACTICS -->
<div class="well well-lg" ng-repeat="foo in campaign.tactics">
<h4>{{foo.name}} - {{foo.type}}</h4>
</div>
<!-- SELECT TACTIC TYPE (Loaded from separate Firebase instance)-->
<select ng-model="tacticSelect">
<option ng-repeat="tactic in tactics" value="{{tactic.name}}">{{tactic.name}} ({{tactic.type}})</option>
</select>
<!-- PUSH NEW TACTICS INTO TACTIC LOOP -->
<button class="btn" ng-click="campaign.tactics.push({'name': tacticSelect, 'type': 'email'})"><span class="glyphicon glyphicon-plus" ></span> Add Tactic</button>
<!-- SAVE CAMPAIGN TO CAMPAIGNS FIREBASE -->
<button class="btn btn-success" ng-click="campaigns.add(campaign)"></span> Save</button><!-- ng-click="campaigns.add(campaign)"-->
CONTROLLER
function scopeAssignments($scope, angularFireCollection, $location){
$scope.tactics = angularFireCollection(fbTactics);
$scope.campaigns = angularFireCollection(fbCampaigns);
// SAMPLE DATA SET
$scope.campaign = {"tactics" : []};
}
DESIRED DATA STRUCTURE (I'm trying to create campaigns that can have multiple tactics with in a single campaign.)
$scope.campaign = {
"name" : "Sample Campaign",
"tactics" : [{
"name" : "First Tactic",
"type" : "Email"
}]
};
add is neither an Angular method nor a DOM method. It's a method provided by an angularFireCollection. angularFireCollection synchronizes data explicitly, which means that calling $scope.campaigns.tactics.push is not sufficient to send that data to Firebase.
You essentially have two options. You can use the angularFire service which will automatically synchronize data whenever it changes locally.
CONTROLLER
function scopeAssignments($scope, angularFire){
$scope.tactics = angularFire(fbTactics);
$scope.campaigns = angularFire(fbCampaigns);
$scope.campaign = $scope.campaigns[campaignID];
$scope.campaign.tactics = [];
}
If you want to stick with angularFireCollection, you have to explicitly notify the collection when you want some data sent to the Firebase server. You can do this, for example, by:
TEMPLATE
<!-- PUSH NEW TACTICS INTO TACTIC LOOP -->
<button class="btn" ng-click="addTactic(campaign, tacticSelect)">
<span class="glyphicon glyphicon-plus"></span> Add Tactic
</button>
CONTROLLER
$scope.addTactic = function(campaign, tactic) {
$scope.campaign.tactics.push({'name': tacticSelect, 'type': 'email'});
$scope.campaigns.update($scope.campaign);
}

fine-uploader resubmit parameters but not file

I'm using fine-uploader to take multiple (large) files and pass the filename along with an additional user-input parameter. I do that by creating a text input box (called 'allele_freq') next to each file and I pass the filename and the allele_freq parameter to my cgi script.
What happens next (or what will happen next) is that I analyse the data in the file, using the allele_freq parameter and then some images are returned to the page for the user to look at.
If the user wants to re-analyse the data with a new allele_freq, all I want to do is to pass the filename along with the new allele_freq, i.e. I don't want to have to upload the file again.
I've pasted my working code below (it uploads multiple files along with user input for each file) and then the code that I can't get to work (it produces a 'resubmit' button, but doesn't appear to do anything), along with some comments/musings within the code.
Any information on how I would do this will be gratefully received. I'm very new to both fine-uploader and Javascript (as you can probably tell), so please feel free to criticise (constructively of course!) any of my code.
Many thanks,
Graham
<link href="fineuploader/fineuploader-3.6.4.css" rel="stylesheet">
<script src="fineuploader/jquery-2.0.1.js"></script>
<script src="fineuploader/jquery.fineuploader-3.6.4.js"></script>
<div id="multiFineUploader"></div>
<div id="triggeredUpload" class="btn btn-primary" style="margin-top: 10px;">
<i class="icon-upload icon-white"></i> Upload now
</div>
<script>
$('#multiFineUploader').fineUploader({
request: {
endpoint: 'src/lib/upload.cgi'
},
autoUpload: false,
text: {
uploadButton: '<i class="icon-plus icon-white"></i> Select Files'
}
})
.on('submitted', function(event, id, name) {
var fileItemContainer = $(this).fineUploader('getItemByFileId', id);
$(fileItemContainer)
.append('<input type="text" name="allele_freq">');
})
.on('upload', function(event, id, name) {
var fileItemContainer = $(this).fineUploader('getItemByFileId', id),
enteredAlleleFreq = $(fileItemContainer).find('INPUT[name="allele_freq"]').val();
$(this).fineUploader('setParams', {allele_freq: enteredAlleleFreq}, id);
});
$('#triggeredUpload').click(function() {
$('#multiFineUploader').fineUploader('uploadStoredFiles');
});
</script>
above code works fine
code below doesn't
<div id="resubmitFreqs"></div>
<div id="retry" class="btn btn-success" style="margin-top: 10px;">
<i class="icon-upload icon-white"></i> Resubmit
</div>
<script>
$('#resubmitFreqs').fineUploader({
request: {
//use a different script as shouldn't need to handle all the upload stuff
endpoint: 'src/lib/resubmit.cgi'
}
)}
//get the information from the allele_freq box. Should it still be in scope?? If not, how do I get at it?
.on('upload', function(event, id, name) {
var fileItemContainer = $(this).fineUploader('getItemByFileId', id),
enteredAlleleFreq = $(fileItemContainer).find('INPUT[name="allele_freq"]').val();
$(this).fineUploader('setParams', {allele_freq: enteredAlleleFreq}, id);
});
$('#retry').click(function() {
//I presumably don't want to use 'uploadStoredFiles', but I'm not sure how to post my new parameters into the resubmit.cgi server-side script
$('#resubmitFreqs').fineUploader('uploadStoredFiles');
});
</script>
It seems like you are trying to bend Fine Uploader into something that it is not. Fine Uploader should probably not be involved with this step of your process, as its job is to upload files to your server. It is not meant to be an all-in-one web application. If you want to send additional data to your server at some point in time after the file has been sent, simply send a POST request with that data via XHR.

Passing selected value of a select to form action

I have a menu that I want to differ based on which account is currently selected in the system.
I have a page that allows a user to select an account from an html select. When the user submits the form from the account selection page I want to call the menu method on my controller passing in the selected value so my url looks correct.
Here is the existing template from the page that allows a user to select an account:
#helper.form(action = routes.Accounts.menu {
<table>
<tr>
<td><select id="accountNames">
#accountNames.map { name =>
<option value="#name">#name</option>
}
</select></td>
</tr>
<tr>
<td>
<p>
<input type="submit" value="Choose">
</p>
</td>
</tr>
</table>
}
From my routes file:
GET /account/:accountName/menu controllers.Accounts.menu(accountName: String)
How do I reference the selected value from my select (id="accountNames") and pass it into my form action?
Actually I think you're on the wrong side for doing that.
If the form's action has to change over the use of your 'select', it has to be done using JS.
So when the form is submitted (event submit) you have to update the url.
This can be done easily using javascriptRoutes.
So you have to do several things:
1/ create the javascriptRouter (assuming your add it in Application.scala)
def javascriptRoutes = Action {
Ok(
Routes.javascriptRouter("playRoutes")(
//accounts
controllers.routes.javascript.Accounts.menu
)
).as("text/javascript")
}
2/ define it in your routes file
# Javascript routing
GET /assets/javascripts/routes controllers.Application.javascriptRoutes
3/ add the related javascript file import in your views, let say in main.scala.html
<script type="text/javascript" src="#routes.Application.javascriptRoutes"></script>
4/ add a submit handler to your form that does that before executing the default behavior
$("form").submit(function () {
//this computes the correct URL giving a parameter which is the value of the selected option
var newURl = playRoutes.controllers.Accounts.menu($("#accountNames").val()).url
$(this).attr("action", newUrl);
})
Notice how we've used playRoutes both in the controller (1) and the JS call (4).