English 中文(简体)
利用ock测绘图
原标题:Mapping deeply hierarchical objects to custom classes using knockout mapping plugin

http://knockoutjs.com/documentation/plugins-mapping.html

如果我有多个层次的物体:

var data = {
    name:  Graham ,
    children: [
        {
            name:  Son of Graham ,
            children: [
                {
                    name:  Son of Son of Graham ,
                    children: [
                        {
                            ... and on and on....
                        }
                    ]

                }
            ]
        }
    ]
}

我如何用javascript把它描绘给我的习惯班子:

var mapping = {
    !! your genius solution goes here !!

    !! need to create a myCustomPerson object for Graham which has a child myCustomerPerson object 
    !! containing "Son of Graham" and that child object contains a child myCustomerPerson 
    !! object containing "Son of Son of Graham" and on and on....

}

var grahamModel = ko.mapping.fromJS(data, mapping);

function myCustomPerson(name, children)
{
     this.Name = ko.observable(name);
     this.Children = ko.observableArray(children);
}

www.un.org/Depts/DGACM/index_spanish.htm 绘制地图的原始图能否将这一数据重新描绘成我习惯目标的era子?

最佳回答

与此类似(

CSS:

.left {
    float: left;
}

.clear {
    clear: both;
}​

传真:

<p>Current:&nbsp;
    <a href="#" data-bind="visible: (stack.length > 0), text: selectedNode().name, click: selectParentNode"></a>
    <span data-bind="visible: (stack.length <= 0), text: selectedNode().name"></span>
</p>
<p class="left">Children:&nbsp;</p>
<ul class="left" data-bind="template: {name:  childList , foreach: selectedNode().children}"></ul>

<script type="text/html" id="childList">
    <li data-bind="click: function(){nodeViewModel.selectChildNode($data)}">
        <a href="#">A${name}</a>
    </li>
</script>

<br /><br />
<ul class="clear" data-bind="template: {name:  backBtn }"></ul>

<script type="text/html" id="backBtn">
    <a href="#" data-bind="visible: $data.selectedNode().back, click: function() { nodeViewModel.selectBackNode($data.selectedNode().back) }">Back</a>
</script>​

Java:

var node = function(config, parent) {
    this.parent = parent;
    var _this = this;

    var mappingOptions = {
        children: {
            create: function(args) {
                return new node(args.data, _this);
            }
        }
    };

    ko.mapping.fromJS(config, mappingOptions, this);
};

var myModel = {
    node: {
        name: "Root",
        children: [
            {
            name: "Child 1",
            back: 1,
            children: [
                {
                name: "Child 1_1",
                back: 1,
                children: [
                    {
                    name: "Child 1_1_1",
                    back: 4,
                    children: [
                        ]},
                {
                    name: "Child 1_1_2",
                    back: 2,
                    children: [
                        ]},
                {
                    name: "Child 1_1_3",
                    back: 1,
                    children: [
                        ]}
                    ]}
            ]},
        {
            name: "Child 2",
            back: 1,
            children: [
                {
                name: "Child 2_1",
                back: 1,
                children: [
                    ]},
            {
                name: "Child 2_2",
                back: 1,
                children: [
                    ]}
            ]}
        ]
    }
};

var viewModel = {

    nodeData: new node(myModel.node, undefined),

    selectedNode: ko.observable(myModel.node),

    stack: [],

    selectBackNode: function(numBack) {

        if (this.stack.length >= numBack) {
            for (var i = 0; i < numBack - 1; i++) {
                this.stack.pop();
            }
        }
        else {
            for (var i = 0; i < this.stack.length; i++) {
                this.stack.pop();
            }
        }

        this.selectNode( this.stack.pop() );
    },

    selectParentNode: function() {
        if (this.stack.length > 0) {
            this.selectNode( this.stack.pop() );
        }
    },

    selectChildNode: function(node) {
        this.stack.push(this.selectedNode());
        this.selectNode(node);
    },

    selectNode: function(node) {
        this.selectedNode(node);
    }

};

window.nodeViewModel = viewModel;
ko.applyBindings(viewModel);​

这一样本只是描绘了一套精彩的JSON数据,我可以说,实际上在应用这一精干的准确代码时使用这一数据。

一些额外职能,如:

选择不适用

允许你回树。

母子标签虽然是个环节,可以达到一个层次,但一些叶子的子子有后 but子,允许他们将树子退到一定水平。

页: 1

如果贵国的叶分节没有儿童阵列,那么就会出现在模型中引入新数据的问题。

问题回答

From my experience, I would say that it shouldn t have any problems.

I would use the following line -

var grahamModel = ko.mapping.fromJS(data);

然后,在下行上打一分点,看一看贵挖土中产生的物体(平均或FF+Firebug最佳工程)。 这样,你将知道科。 绘制地图将产生符合你们需要的视角模型。

通常,它会产生一个目标,即只有终点点(具有价值的不同点)为ko.observables。 您通过数据可以用于导航的任何其他数据时间,如<代码>。 儿童: [...... 页: 1

如果你不希望封顶的地图标(为每个节点打一个K地图标),那么你就可以利用“Kko绘图”办法,使你能够接触母物体。 与此类似:

function Folder(parent,data) {
    var self = this;
    self.parent = parent;
    ko.mapping.fromJS(data, self.map, self);
}

Folder.prototype.map = {
     folders : {
        create: function(options) {
            var folder = new Folder(options.parent,options.data);
            return folder;
        }
    }
}

var data = { name:"root", folders: [ {name:"child", folders: [] } ] };
var root = new Folder(null, data);

这样,你只有一份地图副本,其类别原型(或可能是任何功能)。 如果你想要Folder的话。 如果父母也是可以观察到的话,你可以做<条码>数据. 父母;在地图功能中,而不是作为参数通过。 Folder Constructionor, or do that within the chainer Constructionor otherwise than Self.other = parent;

我在

View Model

var Category = function(data, parent) {
    var self = this;
    self.name = data.name;
    self.id = data.id;
    self.parent = parent;
    self.categoryChecked = ko.observable(false);
    ko.mapping.fromJS(data, self.map, self);
};

// This will add a "map" to our category view model
Category.prototype.map = {
     sub_categories  : {
        create: function(options){
            var category = new Category(options.data, options.parent);
            category.parent.categoryChecked.subscribe(function(value){
                category.categoryChecked(value);
            });
            return category;
        }
    }
};  

传真(概览)

    <div data-role="panel" id="left-panel" data-position="left" data-position-fixed="false" data-theme="b">
            <div data-role="collapsible-set" data-bind="template: {name:  category_collapsible , foreach: sub_categories}" data-mini="true" id="categories" data-iscroll> </div>
        </div><!-- END left panel -->

        <script type="text/html" id="category_collapsible">
            <div class="category_collapsible" data-mini="true" data-content-theme="b" data-inset="true" data-iconpos="right">
                <h3>     
                    <input data-role="none" data-them="b" data-bind= checked: categoryChecked, jqmChecked: true, attr: {id: "category_checkbox_"+id}  class="chk_category" type="checkbox" />
                    <label data-bind= attr: {for: "category_checkbox_"+id} ><span data-bind="text: name"> </span></label>
                </h3>
                <ul data-role="listview" data-bind="template: {name:  category_list , foreach: sub_categories}">

                </ul>
            </div>
        </script><!-- END category_collapsible template -->

        <script type="text/html" id="category_list">
            <!-- ko if: sub_categories().length==0 -->
                <li data-theme="c">
                    <input data-role="none" data-theme="c" data-bind= checked: categoryChecked, jqmChecked: true, attr: {id: "category_checkbox_"+id}  class="chk_category" type="checkbox"/>
                    <label data-corners="false" data-bind= attr: {for: "category_checkbox_"+id} >
                        <span data-bind="text: name"> </span>
                    </label>        
                </li>
            <!-- /ko -->
            <!-- ko if: sub_categories().length>0 -->
                <li data-theme="c" data-bind="template: {name:  category_collapsible , data: $data}"></li>
            <!-- /ko -->
        </script>




相关问题
Matrix to Represent a Triangle in Screen Space

So i have a set of four points in 3D Space. P1 [0, 0, 0] P2 [128, 0, 0] P3 [0, 128, 0] P4 [128, 128, 0] Which I m then projecting orthographically to the screen effectively giving me two ...

Using LINQ To SQL with multiple database tables

I have an employee class with as follows: public class Employee { public string FirstName { get; set; } public string LastName { get; set; } public string UserName { get; set; } ...

Mapping points from Euclician 2-space onto a Poincare disc

For some reason it seems that everyone writing webpages about Poincare discs is only concerned with how to represent lines and measure distances. I d like to morph a collection of 2D points (as ...

Fluent NHibernate, varbinary(max) and SQLite

I have a varbinary field in my sql server database that needs to be varbinary(max). I create my database with NHibernate and I use Fluent Nhibernate for my mappings. I also use SQLite for my unit ...

Javascript redirect

I am working with javascript and here is what I am trying to do: 1. Send a get request which looks like "http://localhost:8080/myapp/verify.htm?verifyId=Agkvhs" My requests reaches my verify.jsp in ...

热门标签