I have a multi-form that I am trying to build using ember.js. I've created the first step of the form using an array of data in my ObjectController but I now need to get the data from a JSON object provided by a rails app. I'm having a bit of trouble understanding how I can use ember-data to retrieve the JSON object and load just the options data into my select menu.
Here's my HTML:
<div class="col-md-12 col-xs-12 content">
<div class="text-center center-block">
<div class="col-md-12">
<span>Which of these</span>
<h2>Activities Appeal</h2>
<span>to you the most?</span>
<div class="form-group single-step">
{{view Ember.Select
content=studies
optionValuePath="content.value"
optionLabelPath="content.label"
prompt="Area of Study"
name="answer[3]"
id="answer_3"
class="form-control span12"
}}
</div>
</div>
</div>
Here's my JS:
var Mobile = Ember.Application.create({
LOG_TRANSITIONS: true
});
/** Setup the urls for the mobile screens **/
Mobile.Router.map(function(){
this.resource('index', { path: "/" });
});
/* Extend the controllers to load objects */
Mobile.IndexController = Ember.ObjectController.extend({
selecedStudies:null,
studies: [
{value:'14', label: 'Business'},
{value:'15', label: 'Criminal Justice'},
{value:'16', label: 'Education'},
{value:'17', label: 'Engineering & Science'},
{value:'18', label: 'Fire & Emergency Mgmt'},
{value:'19', label: 'Healthcare'},
{value:'20', label: 'Information Technology'},
{value:'21', label: 'Legal/Paralegal Studies'},
{value:'22', label: 'Liberal Arts'},
{value:'24', label: 'Psychology & Counseling'},
{value:'25', label: 'Social Services'},
{value:'26', label: 'Web/Graphic Design'},
{value:'27', label: 'Theology'},
{value:'23', label: 'Nursing'}
]
});
And Here is the JSON data as it will come from the rails app:
{"form_id":17,"questions":[{"question":{"id":3,"identifier":"","name":"Area of Study","question_validation_id":null,"require_question":true,"set_as_qualification":false,"type_id":3,"from_option":true,"options":[{"option":{"id":14,"name":"Business"}},{"option":{"id":15,"name":"Criminal Justice"}},{"option":{"id":16,"name":"Education"}},{"option":{"id":17,"name":"Engineering & Science"}},{"option":{"id":18,"name":"Fire & Emergency Mgmt"}},{"option":{"id":19,"name":"Healthcare"}},{"option":{"id":20,"name":"Information Technology"}},{"option":{"id":21,"name":"Legal/Paralegal Studies"}},{"option":{"id":22,"name":"Liberal Arts"}},{"option":{"id":24,"name":"Psychology & Counseling"}},{"option":{"id":25,"name":"Social Services"}},{"option":{"id":26,"name":"Web/Graphic Design"}},{"option":{"id":27,"name":"Theology"}},{"option":{"id":23,"name":"Nursing"}}],"option_view":"grid"}}]}
The end goal is to be able to get and post data for each step in a multi-form but for now I just need to be able to get my data from the JSON object and display it in the step of the form.
I have a working example of my static form available here: http://jsbin.com/qefod/5/edit
Any insight on the proper Ember way to loading a JSON object with Ember Data would be much appreciated.
::: UPDATE :::
I have a bit of an update where I've changed the routes to reflect the source data nesting and I've added model definitions and API request. You can see the updated JSBin here: http://jsbin.com/qefod/7/edit
What I need to do in this example is to load the options from the JSON data into the select menu. For some reason my Ember console does load the data into the model but I can not access it through the template.
To get the value selected in your select element, you need to add value to your view Ember.select like so:
{{view Ember.Select
content=studies
optionValuePath="content.value"
optionLabelPath="content.label"
prompt="Area of Study"
name="answer[3]"
value=selecedStudies
id="answer_3"
class="form-control span12"
}}
Then you can access this inside your step2 action in your controller:
actions: {
step2: function() {
console.log(this.get('selecedStudies'));
}
}
Related
I am trying to post an array of multiple geolocation addresses (generated by Google maps) to my server. The array displays correctly as an array while on the client side, however, the array arrives at the server as a string and not as an array. I am not entirely sure why this happens, also I can't figure out how to convert this string back into an array on the server.
Find below the code of the array while on the client side via the browser:
console.log("Geolocations: " ,selecedAddresses);
This codeabove yields an array of 3 items as illustrated below:
Geolocations: (3) ['Kileleshwa, Nairobi, Kenya', 'Ntinda, Kampala, Uganda', 'Uhuru Highway, Nairobi, Kenya']
When the same array is expanded for better readability:
0: "Kileleshwa, Nairobi, Kenya"
1: "Ntinda, Kampala, Uganda"
2: "Uhuru Highway, Nairobi, Kenya"
length: 3
For me to be able to POST the array to the server, I assign the array value to an HTML input field in a form.
document.getElementById('locationArray').value = selecedAddresses;
Find below the code to my HTML form where input field resides:
<form action="/advertiser" method="POST" enctype="multipart/form-data">
<div class="mb-3">
<input type="text" name="locationArray" value="" hidden/>
</div>
<button type="submit" class="btn btn-secondary"> Add location </button>
</form>
When the user clicks on the Add location button, the array is submitted to the server.
And now for the server side code...
app.route('/Geolocations')
.post((req, res)=>{
let locationArray = req.body.locationArray;
console.log("locationArray: " +locationArray);
})
The code above yeilds:
locationArray: "Kileleshwa, Nairobi, Kenya, Ntinda, Kampala, Uganda, Uhuru, Highway, Nairobi, Kenya"
Instead of the desired out put:
['Kileleshwa, Nairobi, Kenya', 'Ntinda, Kampala, Uganda', 'Uhuru Highway, Nairobi, Kenya']
Looking forward to your help.
I am creating a Risk Management System using Nodejs and Express with Handlebar views. Using SQL, I am extracting two queries in my GET router and rendering them to the handlebar view. This is how it looks:
const parametro = await pool.query('select * from parametro where parid = ?', [parid]);
const tipoParametros = await pool.query('select distinct tipoparid from tipoparametro order by tipoparid');
res.render('parametros/modificar', {parametro, tipoParametros});
I consoled logged both arrays and they look like these:
- parametro: [
RowDataPacket {
parid: 22,
parcodigo: 'TEST',
tipoparid: 11,
pardescripcion: 'Just a Test',
parexplicacion: 'Test',
parclasificacion: 3,
parvalor: 'Test 1',
parusrcreaid: null,
parfechacrea: 2022-06-25T21:58:19.000Z,
parusrmodid: null,
parfechamod: null
}
]
- tipoParametros: [ RowDataPacket { tipoparcodigo: 'SYS', tipoparid: 11 } ]
However, since my POST for this view needs :id, I am iterating with each parametro because that's how I keep track of each row in my database, with parid. The .hbs view looks like this:
{{#each parametro}}
<form class = "row g-3 needs-validation" method = "POST" action = "/parametros/modificar/{{this.parid}}" novalidate>
{{> formparametro}}
</form>
{{/each}}
and inside "formparametro" there is a form-floating section where I try to iterate with each tipoParametro but nothing is coming out. Looks like this:
<div class="col-6">
<div class="form-floating">
<select class="form-select" id="floatingSelect" name="tipoparid" aria-label="Floating label select example" required>
{{#each tipoParametros}}
<option value={{this.tipoparid}}>{{this.tipoparcodigo}}</option>
{{/each}}
</select>
<label for="floatingSelect">Tipo Parametro</label>
</div>
</div>
How can I have access to the query and columns of tipoParametro?
As your formparametro partial is rendered within your {{#each parametro}} block, the context for each time the partial is rendered is the currently iterated item from parametro. Therefore, the partial has no access to tipoParametros.
What we want to do is provide a context to our partial that does contain tipoParametros. This is very simple. The Handlebars documentation covers Partial Contexts and it states that a context can be supplied to the partial as the second parameter within the mustache brackets, as in:
{{> myPartial myOtherContext }}
As your tipoParametros object belongs to your root context, I would recommend using the #root data variable to pass the root context to your partial. The line in your template invoking your partial thus becomes:
{{> formparametro #root}}
I have created a fiddle for your reference.
I am trying to make an autocompleting search bar. What I have until now is:
<div class="ui search">
<div class="ui inverted transparent icon input">
<input class="prompt" type="text" placeholder="Search...">
<i class="search icon"></i>
</div>
</div>
<script>
$('.ui.search')
.search({
apiSettings: {
url: '/search?key={query}'
},
});
</script>
And router.get('/search',...
This one does a database search (mongoose) and returns an array with all the documents which name contains the search string and does: res.send(thearray). But this doesn't work, is it right to use res.send and is the script right?
Please see examples in Semantic UI: https://semantic-ui.com/modules/search.html#/examples
$('.ui.search')
.search({
apiSettings: {
url: '//api.github.com/search/repositories?q={query}'
},
fields: {
results : 'items',
title : 'name',
url : 'html_url'
},
minCharacters : 3
})
;
You need to map the response from your API into the matching fields required for search.
I am using an array of objects to populate info in a directive, but I do not know, when looping through that array, how to insert each object in the array into the "info" parameter of my directive.
Basically, the HTML template uses data passed to it in the info parameter, like so:
<directive info="{{angular object}}"></directive>
I know how to do this when I've hard-coded individual objects in the controller, like so:
$scope.object1 = {...values here...}
$scope.object2 = {...values here...}
etc.
For this I use:
<directive info="object1"></directive>
<directive info="object2"></directive>
But when I'm looping, I can't do that because I need to repeat the directive for each object in the array.
Here's what i've got
<div class="main" ng-controller="MainController">
<div class="container">
<div class="card" ng-repeat="app in apps">
<app-info info="{{app}}"></app-info> <!--HERE IS MY PROBLEM-->
</div>
</div>
</div>
Obviously I have no idea how or what to put as a value for the "info" parameter.
My MainController is as follows:
$scope.apps = [
{
icon: 'img/move.jpg',
title: 'MOVE',
developer: 'MOVE, Inc.',
price: 0.99
},
{
icon: 'img/gameboard.jpg',
title: 'Gameboard',
developer: 'Armando P.',
price: 1.99
},
{
icon: 'img/forecast.jpg',
title: 'Forecast',
developer: 'Forecast',
price: 1.99
},
{
icon: 'img/shutterbugg.jpg',
title: 'Shutterbugg',
developer: 'Chico Dusty',
price: 2.99
}];
The result is empty, meaning when I load the webpage, there is nothing there.
Just remove brackets. You were close.
<div class="main" ng-controller="MainController">
<div class="container">
<div class="card" ng-repeat="app in apps">
<app-info info="app"></app-info> <!--HERE IS NO PROBLEM-->
</div>
</div>
</div>
I'm new to AngularJS but I love the framework.
What I have right now, is a (stub) single page that loads json data.
JS
var merlinoApp = angular.module('merlino', []);
merlinoApp.controller('mainController', function ($scope, $http) {
...
$http.get('#Url.Action( "consoledatapull", "ConsoleElaborazioni")')
.then(function (res) {
$scope.jobs = res.data.jsonjobs;
$scope.clienti = res.data.jsonclienti;
$scope.console = res.data.jsonconsole;
});
...
});
HTML
<div ng-repeat="roll in jobs | orderBy:sortType:sortReverse | filter:searchJob | filter:searchCliente | filter:searchStato" class="console-row my-row">
...
<div class="console-cell-id console-cell console-cell-padding console-cell-no-border-sx">{{ roll.id }}</div>
...
<div ng-click="collapsed=!collapsed" ng-class="{'console-cell-esito-selected' : collapsed}" class="console-cell-esito console-cell console-cell-no-border-sx">SHORT DESC</div>
<div ng-show="collapsed" class="console-cell-esito-long console-cell console-cell-no-border-sx">{{ roll.esito }}</divng-show></div>
</div>
This populates ng-repeat, and the ng-click shows/hides the `ng-show div.
So far so good(?).
What Ì'm trying to achieve, is to load json data into
<div ng-show="collapsed" class="console-cell-esito-long...
if
<div ng-click="collapsed=!collapsed" ng-class="{'console-cell...
is clicked.
That is each div of ng-repeat, can be loaded with specific data:
<ul>
<li ng-repeat="logelem in jsonlog">
{{ logelem.log }}
</li>
</ul>
I thought about using a function:
<div ng-click="function(id)...
and then load json into a div identified by an id, so i used $index...
The result was, being able to load same data into all divs at once :/
Help would be appreciated.
My suggestion woudl be to add the information to the jobs elements itself.
So for example, the ng-click would become:
<div ng-click="loadData(id, roll)">CLICK ME</div>
and then the loadData would be something like:
$scope.loadData = function(id, roll){
// Do something
roll.result = result;
}
and then you can use the result from that object in the view like you would do in other places. You can then for example hide the object where you want the final result until the variable result is defined.
I think this will be the easiest solution.
Update from comments
Why not change the collapsed value in the method? Or you could use a $watch to listen to changes on the collapsed variable.