I'm using responsive calendar in a mvc project.
When setting up the calendar, I need to fill an object called events with my data.
$(".responsive-calendar").responsiveCalendar({
time: '#DateTime.Now.Year.ToString()' + '-' + '#DateTime.Now.Month.ToString()',
events: { //object to fill with my model data
"2013-04-30": { "number": 5, "url": "http://w3widgets.com/responsive-slider" },
"2013-04-26": { "number": 1, "url": "http://w3widgets.com" },
"2013-05-03": { "number": 1 },
"2013-06-12": {},
"2015-06-12": { "number": 1 }
}
});
However this object isn't an array. How to achieve this
Update :
My model is a list of DateEvents :
class DateEvents
{
DateTime Date {get;set;}
int Count {get;set;}
}
When you have an array with data, but must supply it like in your example in the 'events' property, you can create an object like below.
var myEvents = {};
myEvents["2013-04-30"] = { "number": 5, "url": "http://w3widgets.com/responsive-slider" };
If you can do this for one item, you can do this also in a loop (forEach) to get the data from an existing source (array?) and add it to the myEvents object. After completion of 'myEvents', you can set the value of 'events' of the responsiveCalendar to 'myEvents'.
Related
How can I query elastic search based on the number key?
JSON field name
years_of_experience :
"{\"61\": \"10\", \"8240\": \"5\", \"8249\": \"2\", \"50\": \"0\", \"2079\": \"2\"}"
I want to filter years_of_experience like 50:0.
So, according to your sample, you have documents like the below:
POST myindex/_doc
{
"years_of_experience": {
"50": "0",
"61": "10",
"2079": "2",
"8240": "5",
"8249": "2"
}
}
So, you have an object for years_of_experience, and you want to do an exact match with the field name and values. You need to set all fields inside this field you want to set as a keyword type. First, you need to handle the mapping part of this problem. Here is a solution for this :
PUT myindex
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"dynamic_templates": [
{
"strings_as_keyword": {
"match_mapping_type": "string",
"path_match": "years_of_experience.*",
"mapping": {
"type": "keyword"
}
}
}
],
"properties": {
"years_of_experience": {
"type": "object"
}
}
}
}
While creating your index for this data, you need to use a dynamic template for the years_of_experience object. And all the fields inside this will be keyword type, and you can run term queries on these fields.
So now we can create the documents after creating an index with the above settings. And you can filter the data as below :
GET myindex/_search
{
"query": {
"term": {
"years_of_experience.50": "0"
}
}
}
I've got a lambda function, which acts as a trigger on a table with best scores of users to handle a leaderboard table.
In my leaderboard table, the sort key is the score, and the player's name is a separate entry with a list, because it's possible that there could be more than one player with the same score. Never mind.
So when adding a player I do:
var paramsNewEntry = {
"TableName": leaderboardTable,
"Key": {
"trackId": trackId,
"time": newValue
},
"UpdateExpression": "SET players = list_append(if_not_exists(players, :emptyList), :playersList),
"ExpressionAttributeValues": {
":playersList": [userId],
":emptyList":[]
},
"ReturnValues": "NONE"
};
And this works fine. I wanted to remove it this way:
var paramsOldEntry = {
"TableName": myTable,
"Key": {
"trackId": trackId,
"time": oldValue
},
"UpdateExpression": "DELETE players :playerToRemove",
"ExpressionAttributeValues": {
":playerToRemove": [userId]
},
"ReturnValues": "ALL_NEW"
}
But I get: Invalid UpdateExpression: Incorrect operand type for operator or function; operator: DELETE, operand type: LIST error.
The players attribute is a list, query response example:
{
"Items": [
{
"time": {
"N": "99994"
},
"players": {
"L": [
{
"S": "krystianPostman2"
}
]
},
"trackId": {
"S": "betaTrack001"
}
}
],
"Count": 1,
"ScannedCount": 1,
"LastEvaluatedKey": {
"time": {
"N": "99994"
},
"trackId": {
"S": "betaTrack001"
}
}
}
I've not seen any question on SO which would provide any details on this in javascript, when using the dynamodb Document API.
DynamoDB API doesn't have an option to delete the value from LIST datatype based on its value. However, if you know the index of the value to be deleted, you can use REMOVE to delete the entry from list.
The DELETE action only supports Set data types.
UpdateExpression: 'REMOVE players[0]'
If the LIST is going to have only name attribute, it is better to save it as SET rather than LIST DynamoDB datatype.
Creating Set:-
var docClient = new AWS.DynamoDB.DocumentClient();
docClient.createSet( ["v1", "v2"]);
Deleting the values from SET using DELETE
var obj={"firstName":"John","lastName":"Smith","isAlive":true,"age":25,"address":{"streetAddress":"21 2nd Street","city":"New York","state":"NY","postalCode":"10021-3100"},"phoneNumbers":[{"type":"home","number":"212 555-1234"},{"type":"office","number":"646 555-4567"},{"type":"mobile","number":"123 456-7890"}],"children":[],"spouse":null};
I want to access the phoneNumbers field
So I use
phone=obj.phoneNumbers;
I get an array but without "phoneNumbers" field.I want to get someting like this:
{
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
},
{
"type": "mobile",
"number": "123 456-7890"
}
]
}
You have to create a new object then
var phone = { "phoneNumbers": obj.phoneNumbers };
You can add that part in:
var phone = {"phoneNumbers" : obj.phoneNumbers};
Although there should be a good reason for doing this (like, need to pass it to an API that expects exactly "x"). A single-property object is about as useful as the value of its single property.
How about making a function and using it for other similar purposes:
function transform (prop, payload) {
return { [prop]: payload };
}
And use it like:
phone = transform('phoneNumbers', obj.phoneNumbers);
I'm trying to add data to the end of an observable array but it's just not working as expected. I bet it is something minor but I just can't get my head around it.
What I am doing:
self.businesses = ko.observableArray();
function Business(business) {
var self = this;
self.BusinessID = ko.observable(business.BusinessID );
self.Type = ko.observable(business.Type);
self.Location = ko.observable(business.Location);
}
/*ajax get array of businesses as follows:
[
{
"$id": "1",
"BusinessID ": 62,
"Type": "Data",
"Location": "Data"
},
{
"$id": "2",
"BusinessID ": 63,
"Type": "Data",
"Location": "Data"
},
{
"$id": "3",
"BusinessID ": 64,
"Type": "Data",
"Location": "Data",
} ]
*/
var mappedBusinesses = $.map(data, function (business) { return new Business(business) });
self.businesses(mappedBusinesses);
This all works as expected and the obersablearray is populated.
However if I go to add another business, it wont work. For example, if I call the ajax that returns this (as newBusiness):
{
"$id": "1",
"BusinessID ": 68,
"Type": "Data",
"Location": "Data"
}
and I do:
self.businesses().push(newBusiness);
It adds to the array as an "Object" not a Business. So I thought I would do:
var bus = $.map(newBusiness, function (business) { return new Business(business) });
self.businesses().push(bus);
But I get the error in the JS console "Uncaught TypeError: Cannot read property 'BusinessID' of null
So I made a new var and added the brackets: [] in and it adds to the observable array but not as a "Business" object but rather as an "Array[1]" object at the end and this doesn't function as per the others. Code as follows:
var newBus = {
BusinessID: newBusiness.BusinessID,
Type: newBusiness.Type,
Location: newBusiness.Location
}
var bus = $.map(newBus, function (business) { return new Business(business) });
self.businesses().push(bus);
As mentioned this adds to the observable array but doesn't actually add as a "business" object but rather as an "array[1]" object.
I bet it's something so basic but just can't get it working!
Argh I knew it would be simple!
It was posting the whole array to the ObservableArray...not just the object.
The fix:
self.businesses.push(newBusiness[0])
Had to add the [0] in to get it to push the actual data into the array, not the object!
Thanks for the answers!
You're evaluating the array with your push:
self.businesses().push(newBusiness);
Observable Arrays have their own array functions, you should just do this (no parens):
self.businesses.push(newBusiness);
See this page: http://knockoutjs.com/documentation/observableArrays.html
I'm using REST adapter, when I call App.Message.find() Ember.js makes call to the /messages to retrieve all messages and expect to see JSON structure like this:
{
"messages": [] // array contains objects
}
However API I have to work with response always with:
{
"data": [] // array contains objects
}
I only found the way1 to change namespace or URL for the API. How to tell REST adapter to look for data instead of messages property?
If this is not possible how to solve this problem? CTO said we can adapt API to use with REST adapter as we want, but from some reason we can't change this data property which will be on each response.
Assuming you are ok with writing your own adapter to deal with the difference, in the success callback you can simply modify the incoming name from "data" to your specific entity -in the case above "messages"
I do something like this to give you and idea of what if possible in a custom adapter
In the link below I highlighted the return line from my findMany
The json coming back from my REST api looks like
[
{
"id": 1,
"score": 2,
"feedback": "abc",
"session": 1
},
{
"id": 2,
"score": 4,
"feedback": "def",
"session": 1
}
]
I need to transform this before ember-data gets it to look like this
{
"sessions": [
{
"id": 1,
"score": 2,
"feedback": "abc",
"session": 1
},
{
"id": 2,
"score": 4,
"feedback": "def",
"session": 1
}
]
}
https://github.com/toranb/ember-data-django-rest-adapter/blob/master/packages/ember-data-django-rest-adapter/lib/adapter.js#L56-57
findMany: function(store, type, ids, parent) {
var json = {}
, adapter = this
, root = this.rootForType(type)
, plural = this.pluralize(root)
, ids = this.serializeIds(ids)
, url = this.buildFindManyUrlWithParent(store, type, ids, parent);
return this.ajax(url, "GET", {
data: {ids: ids}
}).then(function(pre_json) {
json[plural] = pre_json; //change the JSON before ember-data gets it
adapter.didFindMany(store, type, json);
}).then(null, rejectionHandler);
},