2014-02-10 46 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