How can I generate client-side view models for knockout in an ASP.NET MVC project?

后端 未结 2 802
余生分开走
余生分开走 2021-01-31 22:54

I am currently working on an ASP.NET MVC solution and have recently introduced both Knockout (an MVVM JS library) and Wijmo (a set of jQuery UI widgets).

With the introd

相关标签:
2条回答
  • 2021-01-31 23:03

    According to their tutorials it's just a simple .map function

    If this is the ViewModel

    function Task(data) {
        this.title = ko.observable(data.title);
        this.isDone = ko.observable(data.isDone);
    }
    

    And this function get's the data from the server, it uses the .map function to inject the server data right into the VM

    // Data
    var self = this;
    self.tasks = ko.observableArray([]);
    
    // Load initial state from server, convert it to Task instances, then populate self.tasks
    $.getJSON("/tasks", function(allData) {
        var mappedTasks = $.map(allData, function(item) {
            return new Task(item)
        });
        self.tasks(mappedTasks);
    });
    

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

    For auto-bind here's an example

    https://groups.google.com/forum/#!msg/knockoutjs/IJTx37UXQVw/UTrWdEK1C-oJ

    0 讨论(0)
  • 2021-01-31 23:03

    Try this pluggin for visual studio http://visualstudiogallery.msdn.microsoft.com/32c15a80-1c54-4e96-a83f-7cd57573a5d2

    0 讨论(0)
提交回复
热议问题