Telerik Forums
Kendo UI for jQuery Forum
1 answer
105 views
I need to be able to 'map' some certain data incoming to an existing javascript object that called upon the appropriate code of my MVVM framework, so I took my hand at trying it myself and I thought I had done a pretty good job, but I am running into a problem.

Edit

I have also created a jsBin to show this behavior exists even without my mapping. This is something that is occuring in the `set` function of kendo, and I want to find a way to stop it

jsBin


Basically, I draw a "mapping" like this, using Kendo's model system.
var Model = kendo.data.Model.define({
   Id: "Id",
   fields: {
       Id: {
           type: "string",
       },
       Name: {
           type: "string",
       },
       Mutations: [],
       Tags: []
   },
   mapping: {
       Tags: {
           children: function (data) {
               return $.extend(data, {
                   onRemove: function (e) {
                       // execute code
                   }
               });
           }
       },
       Mutations: {
           children: function (data) {
               return $.extend(data, {
                   Label: null,
                   onRemove: function (e) {
                       // execute code
                   },
                   onEdit: function (e) {
                       // execute code
                   },
                   onSave: function (e) {
                       // execute code
                   }
               });
           }
       }
   }
});

I know that on the outside, this looks a bit unnecessary, but it works well for me and fits my thinking process. I'm open to suggestions for other ways to do it, but ...

Anyway, the purpose of this is to "map" the `onRemove` functions to the `Tags` array when it comes in from the server, and to map the `onRemove, onEdit, onSave` functions to each child in the `Mutations` array. I can do this 'after' they are loaded, but I wanted to try this approach to learn more about javascript.

So then, this is my mapping code.
kendo.data.ObservableObject.prototype.fromJSON = function (source, mapping) {
    var name,
        value,
        observable = this;
 
    // if there is mapping given, then pass it through that first
    if (mapping) {
        source = kendo.mapping(source, mapping);
    }
 
    for (name in source) {
        if (observable.hasOwnProperty(name)) {
            observable.set(name, source[name]);
        }
    }
 
}

This will call the following code, which I felt pretty proud of myself for writing, considering this is my first time trying to do anything like this in javascript.
kendo.mapping = function (source, mapping) {
    var name,
        value;
 
    // if the user provides us a mapping, we can use that to help
    // build the objects appropriately
    for (name in source) {
        if (source.hasOwnProperty(name)) {
            // get the value for this property or item
            value = source[name];
 
            // try to determine if this is an array, or just a
            // normal object. That will greatly dictate our choice of behavior
            if (value instanceof Array) {
 
                // if this is an array, then we will try to check for a
                // key in the mapping schema
                if (mapping[name].children) {
 
                    // if we discover a mapping key for this array, we
                    // will use it to reconstruct the array data
                    for (var i = 0; i < value.length; i++) {
                        source[name][i] = mapping[name].children(value[i]);
                    }
                }
            } else {
                // attempt to match any non array type keys
                if (mapping[name]) {
                    source[name] = mapping[name](value);
                }
            }
        }
    }
    return source;
}

Now for a while, I thought this was awesome. It was working how I wanted, as demonstrated here;
// -------------------------------------------------------------
// create a kendo ui grid to show the existing prototypes
// -------------------------------------------------------------
widgets.grid = $('#grid').kendoGrid({
    dataSource: {
        transport: {
            read: {
                url: "/administrator/data/prototypes",
                dataType: "json",
                type: 'GET'
            }
        },
        schema: {
            total: "total",
            data: "data"
        },
        page: 0,
        pageSize: 15,
        take: 15,
        serverPaging: true,
        serverFiltering: true,
        type: "aspnetmvc-ajax"
    },
    pageable: {
        refresh: true,
        pageSizes: true
    },
    selectable: "row",
    columns: [
        {
            field: "Id",
            width: 25,
            title: "Identity"
        },
        {
            field: "Name",
            width: 40,
            title: "Name"
        }
    ],
    change: function (e) {
        // get the selected row from the grid
        var selected = this.select();
        // get the data from the selected row
        var data = this.dataItem(selected);
        // update the model
        viewModel.fromJSON(data.toJSON(), viewModel.mapping);
    },
}).data("kendoGrid");
So this code goes to my controller and gets a JSON list of all of the relevant items from the database (stored in `RavenDB` as JSON). This returns flat items, which is exactly how I want them in the database (since the functions can't be serialized, obviously). But when I get them into my UI, I want those functions. So in the `change` function, I take the data and pass it through my `fromJSON` function, which accepts the mapping defined in the view model.

This works, it works very well I think. But then when I try to go further and have other bindings on the view model.. .like this..
viewModel.Mutations.bind("change", function (e) {
   // do something else
});
This is really normal kendo code. I attach the `change` event to a function so that it runs whenever that `ObservableArray` changes. This works fine in my other pages where I don't use mapping, but on this one, once the mapping runs, it seems to become 'unbound'. I have found that if I put the `bind` code _after_ the mapping is done, it works.

So I have to assume that what is happening is that the code that does the binding is somehow hidden inside of the `ObservableArray`, and that it gets deleted when it runs the mapping.

Can anyone help me with this?
Alexander Valchev
Telerik team
 answered on 26 Nov 2013
1 answer
171 views
Im trying to figure out a way to handle server side filtering where our controllers return viewmodels rather than the actual entity. Because a viewmodel is being bound to the grid, when ever I sort or filter the field that is being sent back to the controller is the property name from the viewmodel and does not match to the entity. Because of this I can't do a dynamic where clause as the where clause contains the field of the viewmodel (CarName) and not the entity (Name). Somehow I need a way to map the filter for the viewmodel fields back to the entity so that I can filter properly.  

Here are the basic steps:
Call Repository Get IQueryable<Car> Entity
Filter and sort Data (With Dynamic Query)
Convert entity to viewmodel with Automapper
Return viewmodel
-- This represents my entity and view model, both the fields are the same but have slightly different names

public
class Car {
    public string Name { get; set; }
}
 
public class CarViewModel {
    public string CarName { get; set; }
}
Just to point out im using the Kendo UI Web and not the MVC wrappers.

Petur Subev
Telerik team
 answered on 26 Nov 2013
1 answer
373 views
I have a kendo grid that I am pulling data from an API (same site)...  I cannot grid to populate with the data.

Here is an example of the json string...

[{"clientID":8,"memoID":14,"memoTime":"2012-05-09T09:04:00","subject":"Letter","memo":"Sent Letter ","initials":"AA","code":1,"private":false,"sticky":false}, ... and so on ...]

The data has no 'structure name' returned so in the schema section below I am not sure what to put?

Here is the code calling the JSON:   (This is from one of your online examples)
<div id="example" class="k-content">
<div id="grid"></div>
  $(document).ready(function() {
                    var grid = $("#grid").kendoGrid({
                        dataSource: {
                               transport: {
                                read: {
                                    url: "/memo/a/8/b/1",
                                    dataType: "json",
               
                                }
                            },
                            schema: {
                                data: function (response) {
                                    return response.data; 
                                }
                          
                            },
                            pageSize: 20,
                            serverPaging: true,
                            serverSorting: true,
                            serverFiltering: true
                        },
                        toolbar: kendo.template($("#template").html()),
                        height: 430,
                        sortable: true,
                        pageable: true,
                        columns: [
                            { field: "MemoID", title: "MemoID", width: 100 },
                            { field: "Subject", title: "Subject" },
                            { field: "Initials", title: "Initials", width: 100 },
                            { field: "Memo", title: "Memo" }
                        ]
                    });

Atanas Korchev
Telerik team
 answered on 26 Nov 2013
3 answers
371 views
Is it possible to not chart values that are Not A Number?  NaN shows in chart as large value, sample attached.
Alexander Popov
Telerik team
 answered on 26 Nov 2013
1 answer
600 views
Hi,

I am developing a webmail client with angularjs and kendoui, it has two panes left for viewing user folders and right for message display / composition.  For displaying the folders I am using kendoui treeview and when they select a node we display the proper messages for that folder.

Problem: When a user choose a folder, Drafts, then clicks on a message to finish composing that message they are unable to click on the Drafts folder node to navigate back to the Drafts folder gridview.

Code:
var theTree = $("#treeview").kendoTreeView({
    dataSource: folderArray,
    template: $("#foldersTemplate").html(),
    expand: function(e) {
        code for grabbing any sub-folders.
    },
    select : function(e) {
        var treeviewInner = e.sender,
            idFolder = treeviewInner.dataItem(e.node).id;
 
        /* set-up angularjs controller scope */
        window.location.href = "#/folder/" + idFolder;  //navigates to the proper folder gridview when node seleced.
    }
});
The issue I'm seeing is that once a node is selected, whenever you re-click on it, the select code does not fire.

Any thoughts on how to work around this?
Alexander Valchev
Telerik team
 answered on 26 Nov 2013
5 answers
408 views
I am using the kendo.dataviz.ui.Chart via the MVC wrapper. I am looking to fire an operation after the chart refreshes...
$("#mychart").data('kendoChart').refresh();
Once the animations, etc, finishes for the refresh, I would like be notified by a promise or chart event.
Daniel
Telerik team
 answered on 26 Nov 2013
1 answer
520 views
Hi,

I have a Grid and want to add a progress bar to row based on a value stored in that row. is this possible?
Thanks,
Chris
Dimiter Madjarov
Telerik team
 answered on 26 Nov 2013
3 answers
188 views
Hi,

I'm trying to get a Bootstrap Metro theme in my application but a page renders with a default Bootstrap theme.
http://demos.kendoui.com/bootstrap/

Not sure if I'm loading all the necessary CSS and in in the right order.

        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

        <style type="text/css">
            html {
                overflow-x: hidden; /* Prevent scroll on narrow devices */
            }
            body {
                padding-top: 60px;
            }
        </style>        

        <link href="app/_libraries/bootstrap-3.0.2/css/bootstrap.min.css" rel="stylesheet" />

        <link href="app/_libraries/kendo-2013-Q3/styles/kendo.common-bootstrap.min.css" rel="stylesheet" />
        <link href="app/_libraries/kendo-2013-Q3/styles/kendo.moonlight.min.css" rel="stylesheet" />
        <link href="app/_libraries/kendo-2013-Q3/styles/kendo.dataviz.min.css" rel="stylesheet" />
        <link href="app/_libraries/kendo-2013-Q3/styles/kendo.dataviz.moonlight.min.css" rel="stylesheet" />
        <link href="app/_libraries/kendo-2013-Q3/styles/kendo.mobile.all.min.css" rel="stylesheet" />

        <link href="app/_resources/styles/application.css" rel="stylesheet" />

Thanks

Iliana Dyankova
Telerik team
 answered on 26 Nov 2013
6 answers
132 views
Hi,
  I see how you are reading the data. How can I use the onclick method to go to another site related to the person I select? I would add the site link in the Json of course.

Thanks,
Amin

Alexander Valchev
Telerik team
 answered on 26 Nov 2013
2 answers
71 views
OK I have a bit of a more complex issue.

I am attempting to use three Keno Items and it is not working well
I have a MVC 4 page
On the View I have PanelBar that separates sections of the form
The last PanelBar Item contains a Kendo Scheduler. (This is almost working, but not the reason form y post)
I am using the Kendo Validator to validate the form.

Here is the issue: I f I have validation on the "Editor" window for the scheduler will not close until I hit cancel (or the close X in the upper right corner). The newly saved event will not be displayed until I refresh the page.

Even if all other form elements pass validation, this behavior continues with the scheduler. If I remove validation the scheduler will operate in an expected fashion.

an ideas on this?

-Martin
Martin
Top achievements
Rank 1
 answered on 25 Nov 2013
Narrow your results
Selected tags
Tags
+? more
Top users last month
Anislav
Top achievements
Rank 6
Silver
Bronze
Bronze
Jianxian
Top achievements
Rank 1
Iron
Marco
Top achievements
Rank 3
Iron
Iron
Iron
Jim
Top achievements
Rank 2
Iron
Iron
Nurik
Top achievements
Rank 2
Iron
Iron
Want to show your ninja superpower to fellow developers?
Top users last month
Anislav
Top achievements
Rank 6
Silver
Bronze
Bronze
Jianxian
Top achievements
Rank 1
Iron
Marco
Top achievements
Rank 3
Iron
Iron
Iron
Jim
Top achievements
Rank 2
Iron
Iron
Nurik
Top achievements
Rank 2
Iron
Iron
Want to show your ninja superpower to fellow developers?
Want to show your ninja superpower to fellow developers?