We have a list with 3 entries (reactiveVar > array). By default the entry A will be checked.
[X] Test A
[ ] Test B
[ ] Test C
Now check all the other entries B and C.
After checking B and C it looks like this:
[X] Test A
[X] Test B
[X] Test C
Now we add a new default todo B1 between B and C by clicking the button "add B1".
Normally this was done by an other user and we are using a mongo collection for the default-todos.
To simplyfy this we are using a reactiveVar that produces the same probleme.
okay, after adding B1, the expected result should be:
[X] Test A
[X] Test B
[ ] Test B1
[X] Test C
BUT the entry B1 is also checked!!???? And I dont no why and how can i fix this.
[X] Test A
[X] Test B
[X] Test B1
[X] Test C
I've made a small repo to reproduce the problem. Please have a look at
https://github.com/planetarydev/test-each-checkbox.git
and here is the template and helper function:
<template name="todos">
<h2>Learn Meteor!</h2>
<ul>
{{#each todo in hTodos}}
<li>
<input type="checkbox" class="action-check" data-id="{{todo.id}}" checked={{hChecked todo.id}}> {{todo.description}}
</li>
{{/each}}
</ul>
<button class="add-todo">add B1</button>
</template>
import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var';
import './main.html';
let tempChecked = {
A: true
}
Template.todos.onCreated(function() {
this.todos = new ReactiveVar([
{id:'A', description:'Test A'},
{id:'B', description:'Test B'},
{id:'C', description:'Test C'}
]);
});
Template.todos.helpers({
hChecked(id){
var isChecked = (tempChecked[id] === true);
console.log('isChecked:', id, isChecked);
return isChecked;
},
hTodos(){
return Template.instance().todos.get();
}
});
Template.todos.events({
'click .action-check': function(event, instance){
var checked = $(event.target).is(':checked');
var id = $(event.target).attr('data-id');
console.log(id, checked);
tempChecked[id] = checked;
},
'click .add-todo'(event, instance) {
// add todo B1 between B and C
instance.todos.set([
{id:'A', description:'Test A'},
{id:'B', description:'Test B'},
{id:'B1', description:'Test B1'},
{id:'C', description:'Test C'}
]);
},
});
Have a look at the console-output the return value of the checkbox-helper for B1 is false.
isChecked: A true <<-- By default A is checked
isChecked: B false
isChecked: C false
B true <<-- checking B
C true <<-- checking C
isChecked: C true <<-- Click Button "add B1"
isChecked: A true
isChecked: B true
isChecked: B1 false <<--- BUT, on the website the checkbox is checked
The problem is that your tempChecked array is not reactive so the helper is not updating. I suggest that you keep the checked state inside your todos reactiveVar (simpler and cleaner) or make tempChecked itself reactive.
Related
I am trying to find guidance in the HERE JS documentation on how to add a custom button to the HERE JS 3.0 map UI we have.
The button is going to move the map to center on the customer's current location (we provide the location outside of HERE functionality and pass it in manually). However, this needs to be triggered by a button on the HERE map itself. But I can't figure out how to attach a button to the map (so far documentation is just about how to customize already existing UI buttons or how to add an info bubble):
https://developer.here.com/documentation/maps/dev_guide/topics/map-controls.html
Is this even possible? Help would be greatly appreciated!
The class H.ui.UI should help you get where you want to reach, and specifically the method addControl (name, control). and using the HERE UI kit makes it easy to craft the UI element (i.e. button) they way you wanted too.
I have written the following piece of code to help you achieve what you are looking for (I assume)
//#region add new UI element
inherits = function (childCtor, parentCtor) {
function tempCtor() { } tempCtor.prototype = parentCtor.prototype; childCtor.superClass_ = parentCtor.prototype; childCtor.prototype = new tempCtor(); childCtor.prototype.constructor = childCtor; childCtor.base = function (me, methodName, var_args) {
var args = new Array(arguments.length - 2);
for (var i = 2; i < arguments.length; i++) {
args[i - 2] = arguments[i];
}
return parentCtor.prototype[methodName].apply(me, args);
};
};
var customUI = function (opt_options) {
'use strict'; var options = opt_options || {};
H.ui.Control.call(this);
this.onButtonClick = this.onButtonClick.bind(this);
// create a button element
this.increaseBtn_ = new H.ui.base.Button({
'label': '<svg class="H_icon H_icon" viewBox="0 0 25 25">' +
'<path d="M 18.5,11 H 14 V 6.5 c 0,-.8 -.7,-1.5 -1.5,-1.5 -.8,0 -1.5,.7 -1.5,1.5 V 11 H 6' +
'.5 C 5.7,11 5,11.7 5,12.5 5,13.3 5.7,14 6.5,14 H 11 v 4.5 c 0,.8 .7,1.5 1.5,1.5 .8,0 1.5,' +
'-.7 1.5,-1.5 V 14 h 4.5 C 19.3,14 20,13.3 20,12.5 20,11.7 19.3,11 18.5,11 z" />' +
'</svg>',
'onStateChange': this.onButtonClick
});
//add the buttons as this control's children
this.addChild(this.increaseBtn_);
this.setAlignment(options['alignment'] || 'top-right');
this.options_ = options;
};
inherits(customUI, H.ui.Control);
customUI.prototype.onButtonClick = function (evt) {
'use strict'; if (evt.currentTarget.getState() === 'down') {
console.log('Hollla, I am the new custom UI element.');
}
};
var WaliedCheetos_CustomUI = new customUI();
ui.addControl('WaliedCheetos_CustomUI', WaliedCheetos_CustomUI);
//#endregion
Hello Now i have a problem. I want to insert data to array of objects using for loop
fields:{
type:[Object],
label:"Fields",
optional:true
},
"fields.$.category":{
type:String,
label: "Category"
},
"fields.$.sub":{
type:String,
label:"Sub Category",
},
And using savaData.js in server i tried
// ServiceProviders.update({
// '_id':"GmkGSXjyNFshomdCu"},
// {
// '$set': {'fields':{
// '$.category':categorydata,
// '$.sub':subdata
// }}
// },function(error,result){console.log(error);console.log(x+y);});
and
ServiceProviders.update(
{'_id': 'JN4mRPfJZqBadZtPY' },
{'$set': {'fields.0.category': categorydata,'fields.0.sub':subdata}},
true );
then
var x = "fields."+i+".category";
var y = "fields."+i+".sub";
x=x.toString();
y=y.toString();
ServiceProviders.update(
{'_id': 'JN4mRPfJZqBadZtPY' },
{'$set': {x: categorydata,y:subdata}},
true );
I got Different errors every time could you please help me in this issue
Currently, $ Does not work on Meteor js.
So you have to create a fields object on either server side or client side:
var fieldsData = []
for (var i = 0; i < categorydata.length || i < subdata.length ; i++ ){
fieldsData.push({
category : ( categorydata[i] ? categorydata[i] : '' ),
sub : ( subdata[i] ? subdata[i] : '' )
})
}
ServiceProviders.update(
{'_id': 'JN4mRPfJZqBadZtPY' },
{'$set': {fields : fieldsData}},
true );
Please make sure for either every filds data have cat and subcat value or make cat and subcat cat optional true.
I want to display key-value pairs stored in an array (derived from a Session-JSON variable) by using the #each template directive. How can I get access (if possible) to the fields of objects in an array.
Sorry, if this is a question that has been already answered, but I didn't find an appropriate answer here.
Here is some sample code (part of a template helper):
attributes: function () {
var attributes = [];
attributes = [{key: test1, value: 1}, {key: test3, value: 2}, {key: test3, value: 3}];
return attributes;
},
In the template, I used "this" or "this.key". Both didn't work the way as expected.
Thanks for any tipps!
Have you defined variables test1, test3 ? It looks like you put test1 and test3 without ", so it means js is trying to find variables with such names. That's why you couldn't see this.key working.
var attributes = [];
attributes = [{key: "test1", value: 1}, {key: "test2", value: 2}, {key: "test3", value: 3}];
return attributes;
In template:
{{#each attributes}}
{{this.key}} : {{this.value}}<br>
{{/each}}
Output:
test1 : 1<br>
test2 : 2<br>
test3 : 3<br>
Check here
I have a simple array 'names' with name and id.
I'd like to access the name from other loop (phones).
Code is below:
{{#names}}
{{name}} - {{id}} // this shows for example '123456 - 001'
{{/names}}
{{#phones}}
{{number}} - {{id}}
-->here i'd like to fetch a 'name' from names based on phone {{id}}
{{/phones}}
Is there something like {{../names.id['001']}}??
Is this possible?
I'm doing something like this:
{{#phones}}
{{#../names}}
{{#ifeq ../id this.id}}
{{this.name}}
{{/ifeq}}
{{/../names}}
{{/phones}}
with ifeq being this helper:
Handlebars.registerHelper('ifeq', function (a, b, options) {
if (a == b) { return options.fn(this); }
});
I've looked at this other question, but can't get my select box to work correctly:
Binding initial/default value of dropdown (select) list
I've got the following Game object:
function Game(visitingTeamDetails, homeTeamDetails, game) {
if (arguments.length > 0) {
this.VisitingTeamDetails = visitingTeamDetails;
this.HomeTeamDetails = homeTeamDetails;
this.GameId = ko.observable(game.GameId);
this.HomeTeamName = ko.observable(game.HomeTeamName);
this.VisitingTeamName = ko.observable(game.VisitingTeamName);
this.SportTypeName = ko.observable(game.SportTypeName);
this.HomeAccountName = ko.observable(game.HomeAccountName);
this.VisitingAccountName = ko.observable(game.VisitingAccountName);
this.GameDateString = ko.observable(game.GameDateString);
this.GameTimeString = ko.observable(game.GameTimeString);
this.AvailableSportTypes = ko.observableArray(game.Sports);
this.sportTypeFunction = function () {
for (sportType in this.AvailableSportTypes()) {
if (this.AvailableSportTypes()[sportType].Name == this.SportTypeName()) {
return this.AvailableSportTypes()[sportType];
}
}
return null;
};
this.SportType = ko.observable(game.SportType);
}
}
SportType is an object with Name and SportTypeId.
I have the following template:
<td rowspan="3"><select data-bind="options: AvailableSportTypes, value: SportType, optionsText:'Name', optionsCaption: 'Choose...'" class="sportType"></select></td>
AvailableSportTypes is a list of SportType.
The list is coming in with the names of the SportTypes in the drop down list, but I can't make the initial selection be SportType. I wrote sportTypeFunction to show myself that the data was coming in correctly, and it would select the correct value, but changing my selection in the drop down would not update SportType.
I'm sure I'm doing something wrong. Anyone see it?
Thanks
When game.SportType gets passed in, it needs to be a reference to the an item in the game.AvailableSportTypes and not just an object that looks the same.
Basically two objects are not equal unless they are actually a reference to the same object.
var a = { name: "test" },
b = { name: "test" };
alert(a === b); //false
So, you would need to call your function to locate the correct object in the array and set it as the value of your observable.
Not that it is way better, but in KO 1.3 you can extend .fn of observables, observableArrays, and dependentObservables to add additional functionality.
Here is a sample: http://jsfiddle.net/rniemeyer/ZP79w