I have the following backbone model:
var Info = Backbone.Model.extend({
urlRoot: 'http://localhost:8080/info',
defaults : {
nombre: '',
tipo : '',
telf : 0,
icono : '',
direccion:[{
direccion:'',
latitud:'',
longitud:''
}]
},
idAttribute:"_id"
});
I want to change the value of the "direccion" atribute of the array inside "direccion".
I have used the following code but is not working:
//clone the array
var direccionArray = _.clone(this.collection.get(idInfo).get('direccion'));
direccionArray.direccion = this.$('#dir-info-edit').val();
Here I obtain the array with the values changed and works fine:
console.log(direccionArray);
Now I set the array into my backbone model as follow and is not working (the model don't change) and I get the same model (changing other atributes like "nombre" or "tipo" works fine but not with the array):
this.collection.get(idInfo).set('direccion',direccionArray);
console.log(this.collection.get(idInfo));
Could someone help me please?
As stated direccion attribute of model is an array that contains object at index 0.
When you trying to do:
//clone the array
var direccionArray = _.clone(this.collection.get(idInfo).get('direccion'));
direccionArray.direccion = this.$('#dir-info-edit').val();
This will not update the direccionArray[0], which you want to update, and just will add new attribute to your array object.
What you want to do is:
direccionArray[0].direccion = this.$('#dir-info-edit').val();
Try console.dir(direccionArray) the old array and you will see the difference.
Update:
Please see this jsfiddle with the explanation of the issue. The main reason for your case can be that you are tring to get the value with jquery's val() method on an element that is not an input.
Related
Im trying to bind products of certain categories, in each category there is an expand of products, first of all i list the categories from default oData service read-write only http://services.odata.org/V3/OData/OData.svc, then by clicking the one of category im taking its path( it returns like: Category(1) or Category(2) and etc ), and use it to call the products of this category, but there is a mistake, Aggregation "items" does not exist in Element sap.m.StandardListItem#subcatId what im doing wrong?
here is the code that take the sPath of category then trying to retrieve the list of products of this category:
var app = sap.ui.getCore().byId("appId");
var list = sap.ui.getCore().byId("listId");
var sItem = list.getSelectedItem();
var sPath = sItem.oBindingContexts.data.sPath;
var sCont = sap.ui.getCore().byId("subcatId");
var sCats = new sap.m.StandardListItem({
parameters: {expand: "Products"},
title: "{data>Name}"
})
sCont.bindAggregation("items","data>"+sPath,sCats);
the api's:
http://services.odata.org/V3/OData/OData.svc/Categories
http://services.odata.org/V3/OData/OData.svc/Products
http://services.odata.org/V3/OData/OData.svc/Categories?$expand=Products
thank you all for help!
It seems that sCont is an instance of sap.m.StandardListItem. The StandardListItem does not have an aggregation "items" as you can see from the API docs. Instead of calling
sCont.bindAggregation("items","data>"+sPath,sCats);
you should try to bind the items aggregation of the corresponding list like this:
list.bindAggregation("items",...);
I can see in your code snippet that you even retrieved the list in the second line:
var list = sap.ui.getCore().byId("listId");
Related to: mongodb/meteor collection check if subdocument field exists when field is a variable
I'm trying to query a Meteor collection by building an object with some variable field names. This works when the object has one field, for example
var query = {};
query['myField.'+myVariable] = {$exists: true};
Collection.find(query); //works fine
But I need to query with multiple selectors. For example, I need to check that a field name with a variable exists, and also check if some other field = true, and if some other field = a variable. So I'm trying to find a general way to build query objects. I have tried the following:
var query = {};
query['myField.'+myVariable] = {$exists: true};
query[newField] = false;
Collection.find(query);
This doesn't work. I'm not sure if that's because the 'newField' is not of type Object or something.
I've tried also using the $and selector to see if that works but I don't think the syntax I'm using is exactly correct...
var query = {};
var object = {};
object['myField'.+myVariable] = {$exists: true};
query['$and'] = [object, {newField: false}];
Collection.find(query);
This also doesn't work. I was trying to build with the mongo $and selector which works by using an array.
How do I syntactically build a Meteor collection query with javascript object notation and object literals? I feel like either one of these should work.
To be specific, I'm looking for the following (semi pseudocode since getting a subdocument/subobject with dot notation doesn't work with a mongo query)
Collection.find({correlated: false, readBy.(Meteor.userId()): {$exists: true} ...)
I also thought this should work:
var query = {};
query['myField.'+myVariable] = {$exists: true};
Collection.find(query, {otherField: false})
//OR
var query2 = {};
query['priority'] = false;
Collection.find(query, query2)
But neither of them do.
EDIT: example doc. I want to find the document such that the current user ID is NOT a readBy field AND has correlated: false
{
"_id" : ObjectId("55b6868906ce5d7b1ac6af10"),
"title" : "test",
"correlated" : "false",
"readBy" : {
"DXqLhesDEJq4ye8Dy" : ISODate("2015-07-27T18:29:43.592Z")
}
}
There is only one selector argument to a find. So this:
Collection.find(query, {otherField: false})
is not correct. query would have to contain the information about otherField. Have a close look at this example code:
// 'readBy.abc123'
var key = 'readBy.' + Meteor.userId();
// build the selector by parts
var selector = {correlated: false};
selector[key] = {$exists: false};
// selector should now be something like:
// {correlated: false, 'readBy.abc123': {$exists: false}}
// note that these are ANDed together so all conditions must be true
// here's a cursor you can use to fetch your documents
var cursor = Collection.find(selector);
// log one of the selected documents
console.log(Collection.findOne(selector));
so I have a JSON object returned from a webservice. Now I want to:
get a subset which matches a categoryTitle i pass as parameter (this seems to work)
from my filtered resultset I want to get another array of objects (helpsubjects), and for each of this subjects I want to extract the SubjectTitle.
Problem: It seems my Array of HelpSubjects does not exist, but I can't figure out why and hope you could help.
Perhaps this piece of commented code makes it more clear:
$.fn.helpTopicMenu = function (data) {
that = this;
var categoryContent = contents.filter(function (el) {
return el.CategoryTitle == data.categoryTitle;
});
debug('categorys Content: ', categoryContent); //see below
var container = $('#subjectList');
var subjectList = categoryContent.HelpSubjects;
debug('Subjects in Category: ', subjectList); // UNDEFINED?!
$.each(subjectList, function (i, item) {
container.append(
$('<li></li>').html(subjectList[i].SubjectTitle)
);
});
the line debug('categorys Content: ', categoryContent); returns the following object as shown in the picutre (sadly I can't add a picture directly to the post yet, so here's the link): http://i.stack.imgur.com/0kKWx.png
so as I understand it, there IS actually a HelpSubjects-Array, each entry containing a SubjectTitle (in the picture there actually is only one entry, but I need to have the Artikel einfügen as my html.
Would be great if you can help me.
The variable categoryContent set is an array of objects.
Try debugging categoryContent[0].HelpSubjects and see if you can access the property. If so, you can also loop this array if need be.
from documentation:
<tags-input>
<auto-complete
source="{expression}"
>
</auto-complete>
</tags-input>
The result of the expression must be a promise that eventually resolves to an array of objects.
$scope.loadSuperheroes = function(query) {
// An arrays of strings here will also be converted into an
// array of objects
return $http.get('superheroes.json');
};
But I have already have an array of objects in $scope. But with a different structure:
$scope.superheroes = [{"id":1, "name":"Batman"}, {"id":2, "name":"Superman"}]
How to say in html to use list from $scope.superheroes.name ?
You can change the property used to display the tag text by setting the displayProperty property:
<tags-input ng-model="tags" display-property="name"></tags-input>
That property will also be used by the autocomplete directive to display the returned suggestions.
As described in the docs : http://mbenford.github.io/ngTagsInput/gettingstarted
NgTagsInput can perform autocomplete on an array of basic items (I asked it on GitHub to the creator of that directive months ago).
So I'll think you will have :
to reconstruct your array of objects to something like this : [{ text: 'Tag1' }, { text: 'Tag2' }, { text: 'Tag3' }]
Then, you must use the $query as parameter of your loadSuperheroes() method (as $query is the text that is being typed.
So, the HTML :
<tags-input ng-model="filteredsuperheroes">
<auto-complete
source="loadSuperheroes($query)"
>
</auto-complete>
</tags-input>
Then, the JS (Angular) part :
$scope.filteredsuperheroes = [];
angular.forEach(superheroes, function(superhero) {
var newEntry = {};
newEntry['text'] = superhero.name;
$scope.filteredsuperheroes.push(newEntry);
});
$scope.loadSuperheroes = function(query) {
return $http.get('/filteredsuperheroes?query=' + query);
};
Provide a live example, so I'll could test this :) I'm not sure that will work, but you should easily understand what I mean :)
The autocomplete's source attribute needs a promise, so if you want to use an already existing array of objects you'll have to return a promise that resolves to it:
$scope.superheroes = [{"id":1, "name":"Batman"}, {"id":2, "name":"Superman"}];
$scope.loadTags = function(query) {
var deferred = $q.defer();
deferred.resolve($scope.superheroes);
return deferred.promise;
};
and, as your displayed property name is not text (default by tagsInput), you need to specify it by adding the attribute display-property="name" to tags-input element:
<tags-input ng-model="superheroesModel" display-property="name" add-on-paste="true">
<auto-complete min-length="1" source="loadTags($query)"></auto-complete>
</tags-input>
I forked a simple autocomplete example from ngTagsInput Demo Page and made these changes. Check it out these changes here.
Assume we have the following Object :
var gridViewModelJs =
{"GridViewModel":{"Rows":[{"RowNumber":"1","Id":"6","Name":"FullNameOfUser","NumberOfUsers":"12","RegistrationDate":"10/15/2013"}],"FoundItems":"4","CurrentPage":1,"TotalPages":1,"ItemsPerPage":50,"PagingLinks":""},
"EntityModel":{"Id":0,"PermissionIds":null,"Name":null,"NumberOfUsers":0,"PersianRegistrationDate":null,"RegistrationDate":"0001-01-01T00:00:00","Authorizations":null,"Users":null,"Contents":null}};
var KoEntityViewModel = ko.mapping.fromJS(gridViewModelJs);
ko.applyBindings(KoEntityViewModel);
Above code works, for updating the KoEntityViewModel. I used the following code :
// receivedData is data that returns from jQuery Ajax
// I'm dead sure `receivedData` is correct
var doneFunc = function (receivedData) {
ko.mapping.fromJS(receivedData, KoEntityViewModel.EntityModel);
showDetailsBlock();
};
But nothing update in KoEntityViewModel.EntityModel.
Please guide me how I can update KoEntityViewModel.EntityModel in above sample
When you update the mapping after applying bindings, use three parameters:
ko.mapping.fromJS(receivedData, {}, KoEntityViewModel);