This is a migrated thread and some comments may be shown as answers.

Kendo Mobile Datasource Init on MVVM

1 Answer 159 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Gisli
Top achievements
Rank 2
Gisli asked on 13 Aug 2014, 10:27 AM
hi, i have a simple MVVM listview that get data from a json webservice.:

<div data-role="view"  
     data-model="app.products.viewModel" >
    <ul 
                    data-role="listview" 
                    data-bind="source: productsDS"
                    data-template="product-template">
        
    </ul>                              
</div>

and my viewModel:
(function (global) {
    var ProductViewModel,
        app = global.app = global.app || {};

    ProductViewModel = kendo.data.ObservableObject.extend({   
        productDS: "",        
        init: function() {         
            var that = this,
                dataSource;         
            kendo.data.ObservableObject.fn.init.apply(that, []);
            // Get Session from localStorage when user login.
            var session = getSession();
           
            var dataSource = new kendo.data.DataSource({
            transport: {
                read: {
                    url: "http:/.... my service" +  session,
                    dataType: "json"
                }
            },
            schema: {
               data: "results.data"
            }
        });
            that.set("productDS", dataSource);
       }      
    });

    app.products = {
        viewModel: new ProductViewModel()
    };
})(window);



When i start the app, i get no data from the datasource because session is still "undefined". Session is a value the user get doing a previous login.
If i refresh the app (appBuilder Simulator), then the session is not nul and everything works.

how i can trigger the dataSource when i have the session already?





1 Answer, 1 is accepted

Sort by
0
Alexander Valchev
Telerik team
answered on 15 Aug 2014, 07:28 AM
Hi Gisli,

In order to prevent the automatic DataSource reading you should set the autoBind configuration option of the ListView to false.
In your case you will have to build the read URL address dynamically after the DataSource is initialized and session is retrieved. To achieve that you may define the transport.read.url as a function and pass the session as optional parameter of the read request. For example:

read: {
  url: function(data) { //use function
    return "http:/.... my service" + data.session;
  },
  dataType: "json"
}
 
dataSource.read({ session: "123" }); //pass the session to the read.url function


Regards,
Alexander Valchev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
MVVM
Asked by
Gisli
Top achievements
Rank 2
Answers by
Alexander Valchev
Telerik team
Share this question
or