Friday, June 14, 2013

Kendo datasource

   1: <div id="form-container">
   2:  
   3:     <div class="form" >
   4:                    <select data-role="dropdownlist" data-value-field="ProductID" data-text-field="ProductName" data-bind="source: productsSource, value: selectedProduct"></select>
   5:         <ul>
   6:             <li>
   7:                 <label>ID</label>
   8:                 <span data-bind=  "source: productsSource, events: { change: change }"></span></li>
   9: @*            <li>
  10:                 <label>Name</label>
  11:                 <input type="text" class="k-textbox" id="products" data-bind="value: selectedProduct.ProductName, events: { change: change }" /></li>
  12:             <li>
  13:                 <label>UnitPrice</label>
  14:                 <input type="text" data-role="numerictextbox" data-bind="value: selectedProduct.UnitPrice, events: { change: change }" /></li>*@
  15:         </ul>
  16:  
  17:         <button data-bind="click: remove">Delete Product</button>
  18:  
  19:     </div>
  20: </div>
  21: <script>
  22:     var crudServiceBaseUrl = "@Url.Action("Index", "Duyuru", new { area = "Ayarlar" })";
  23:  
  24:     $(document).ready(function () {
  25:         //setTimeout(function () {
  26:             alert("hi");
  27:         var uniqId = '@(ViewBag.UniqEkId)'
  28:         var messageId = '@(ViewBag.Id)'
  29:         alert(crudServiceBaseUrl);
  30:         var viewModel = kendo.observable({
  31:             productsSource: new kendo.data.DataSource({
  32:  
  33:                 transport: { 
  34:                     read: {
  35:                         contentType: "application/json; charset=utf-8",
  36:                         type: "POST",
  37:                         url: crudServiceBaseUrl + "/Read",
  38:                         dataType: "json",
  39:                         data: { id: messageId }
  40:                     },
  41:                     update: {
  42:                         contentType: "application/json; charset=utf-8",
  43:                         type: "POST",
  44:                         cache: false,
  45:                         url: crudServiceBaseUrl + "/Update",
  46:                         dataType: "json"
  47:                     },
  48:                     destroy: {
  49:                         contentType: "application/json; charset=utf-8",
  50:                         type: "POST",
  51:                         cache: false,
  52:                         url: crudServiceBaseUrl + "/Delete",
  53:                         dataType: "json"
  54:                     },
  55:                     parameterMap: function (options, operation) {
  56:                         if (operation !== "read" && options.models) {
  57:                             var x = " { models :" + JSON.stringify(options.models) + "}";
  58:                             console.log(x);
  59:                             return x;
  60:                         }
  61:                         return options;
  62:                     }
  63:                 },
  64:                 autoSync:true,
  65:                 batch: true,
  66:                 schema: {
  67:                     model: { id: "KayitNo" },
  68:                     data: function (result) {
  69:                         if ($.isArray(result)) {
  70:                             return result;
  71:                         }
  72:                         else {
  73:                             return [result];
  74:                         }
  75:                     }
  76:                 }
  77:             }),
  78:             selectedProduct: null,
  79:             hasChanges: false,
  80:             save: function () {
  81:                 this.productsSource.sync();
  82:                 this.set("hasChanges", false);
  83:             },
  84:             remove: function () {
  85:                 if (confirm("Silmek istediğinize emin misiniz?")) {
  86:                     this.productsSource.remove(this.selectedProduct);
  87:                     this.save();
  88:                 }
  89:             },
  90:          
  91:             change: function () {
  92:                 this.set("hasChanges", true);
  93:             }
  94:         });
  95:         console.log(viewModel);
  96:         kendo.bind($("#form-container"), viewModel);
  97:         });
  98:  
  99:  
 100:     //}, 1);
 101: </script>