2014-02-10 21 views
0

我想在Kendo移動應用程序(即不是MVC)中分離我的視圖和ViewModels。我在ViewModel中有一個遠程數據源並且無法讓它工作 - 我確信它是簡單的(我無法找到在ViewModel中使用遠程數據源的Kendo示例 - 它全部是內聯的(http://demos.telerik.com/kendo-ui/web/mvvm/remote-binding.html,http://docs.telerik.com/kendo-ui/getting-started/framework/datasource/overview) (e){var n = this; return e === t?n._data:(n._data = this._observe(e),n._ranges = [],n ._addRange(n._data),n._total = n._data.length,n._process(n._data)中,t)}」,而不是實際數據。遠程數據源和遠程視圖+ MVVM

games.html查看

<div id="tabstrip-home" 
    data-role="view" 
    data-title="Games" 
    data-model="app.gamesService.viewModel"> 

    <ul class="games-list"    
     data-bind="source: gamesDataSource" 
     data-template="template"> 
    </ul> 

</div> 

<script type="text/x-kendo-template" id="template"> 
    <div class="product"> 
     <h3>#:ProductName#</h3> 
     <p>#:kendo.toString(UnitPrice, "c")#</p> 
    </div> 
</script> 

個games.js視圖模型

(function (global) { 
    var GamesViewModel, app = global.app = global.app || {}; 
  
    GamesViewModel = kendo.data.ObservableObject.extend({ 
                                                            gamesDataSource: new kendo.data.DataSource({ 
                                                                                                           transport: { 
                                                                    read: { 
                                                                                                                   url: "http://demos.telerik.com/kendo-ui/service/Products", 
                                                                                                                   dataType: "jsonp" 
                                                                                                               } 
                                                                } 
                                                                                                       }) 
                                                              
                                                        }); 
    app.gamesService = { 
        viewModel: new GamesViewModel() 
    }; 
})(window); 

回答

0

我發現了一個例子,並設法得到這個工作,雖然JavaScript是一個有點不同。我必須要在

(function (global) { 
    var GamesViewModel, 
     app = global.app = global.app || {}; 

    GamesViewModel = kendo.data.ObservableObject.extend({ 
     gamesDataSource: null, 

     init: function() { 
      var that = this, 
       dataSource; 

      kendo.data.ObservableObject.fn.init.apply(that, []); 

      dataSource = new kendo.data.DataSource({ 
       transport: { 
        read: { 
         url: "http://demos.telerik.com/kendo-ui/service/Products", 
         dataType: "jsonp" 

         //ProductID":1,"ProductName":"Chai","UnitPrice":18,"UnitsInStock":39,"Discontinued":false 

        } 
       } 
      }); 

      that.set("gamesDataSource", dataSource); 
     } 
    }); 

    app.gamesService = { 
     viewModel: new GamesViewModel() 
    }; 
})(window); 
+0

PS讀了 - 我真的不明白這裏的區別 - kendo.data.ObservableObject.extend這個代碼第2位似乎被延長初始化事件 - 我真的需要在另一個事件上公開遠程數據源(例如,點擊一個按鈕) – Rodney