KnockoutJS - 在可观察数组中绑定属性ob对象

时间:2014-05-21 16:39:51

标签: javascript knockout.js

function OutletsView(){
  self = this;
  self.locations = ko.observableArray(<?php echo json_encode($locations); ?>);
}

ko.applyBindings(new OutletsView());

地点位于结构

[
{
  name: 'Demo',
  oid: 1,
  children: [
    {
      id: 2,
      name: 'de'
    },
    {
      id: 2,
      name: 'de'
    }
  ]
},
{
  name: 'Demo',
  oid: 1,
  children: [
    {
      id: 2,
      name: 'de'
    },
    {
      id: 2,
      name: 'de'
    }
  ]
}
]

我需要绑定oid并更改html

<script type="text/html" id="location-filter">
    <div>
        <div data-bind="text: oid"></div>
        <div>
            <ul data-bind="foreach: locations">
                <li data-bind="text: name, click: $parent.applyFilter(id, name, $parent)"></li>
            </ul>
        </div>
    </div>
</script>

所以我想改变oid,但无法找到方法 任何人都可以帮助我吗?

修改

function OutletsView(){
        self.locations = ko.observableArray([]);
        self.getChildLocations = function(id){
            $.post('<?=$this->url(array('controller' => 'location', 'action' => 'get-locations'), 'default', true); ?>',
                {id: id},
                function(data){
                    var parsed = $.parseJSON(data);
                    var item = new Location(parsed);
                    self.locations.push(item);
                }
            );
        }
    }
    function locationItem(el){
        tself = this;
        tself.id = el.id;
        tself.name = el.name;
        tself.lo_idx = el.lo_idx;
        tself.cls = ko.observable(el.cls);
    }

    function Location(item){
        kself = this;
        kself.using_id = 0;
        kself.using_name = item[0].location_type;
        kself.cls = ko.observable('jcsik jactive jsminimize');
        kself.locations = [
            new locationItem({id: 0, name: item[0].location_type, lo_idx: 0, cls: ''}),
            new locationItem({id: 1, name: 'Не выбрано', lo_idx: 0, cls: 'jexpand'})

        ];
        $.each(item, function(i, el){
            kself.locations.push(new locationItem(el));
        });
        kself.getLocation = function(){
            return kself;
        }
    }

2 个答案:

答案 0 :(得分:0)

你在foreach循环之外绑定oid属性导致中断..

您可以更改方法并执行类似

的操作
<script type="text/html" id="location-filter">


        <div>
            <ul data-bind="foreach: locations">
                <li>
                   <span data-bind="text: oid"></span> : 
                   <span data-bind="text: name, click: $parent.applyFilter(id, name, $parent)"></span>
                </li>

            </ul>
        </div>
    </div>
</script>

答案 1 :(得分:0)

首先你的oid在你的foreach循环之外,所以绑定在那时没有访问它,其次如果你想从js更新oid它需要是一个可观察的

两个选项。

  1. 使用ko.mapping插件将属性转换为knockout observables

    函数OutletsView(){   self = this;   self.locations = ko.observableArray(ko.mapping.fromJS()); }

  2. 你需要引用ko.mapping.js插件,这就是它将所有属性转换为ko属性

    1. 创建您自己的转换方法,以转换您想要观察的属性。
    2. 类似这样的事情

      function OutletsView(){
        self = this;
        self.locations = ko.observableArray(toKO(<?php echo json_encode($locations); ?>));
      }
      
      function toKO(arr){
          var retObj = []
          for(var i = 0; arr.length, i++){
             retObj.push([
               name: ko.observable(arr[i].name)
               oid: ko.observable(oid);
               /// and on and on
             });
         }
         return retObj;
      }
      
相关问题