What am I doing wrong here returning viewmodel?

冷暖自知 提交于 2020-01-06 08:26:09

问题


I am trying to convert this to durandal/hottowel.

viewModel:

define(['plugins/router', 'knockout', 'services/logger', 'durandal/app', 'mapping', 'services/routeconfig', 'services/dataBindingHandlers', 'services/appsecurity'], function (router, ko, logger, app, mapping, routeconfig, dataBindingHandlers, appsecurity) {
    function activate() {
        logger.log(viewModel.title + "'s  View Activated", null, viewModel.title, true);
        return true;
    }

    var viewModel = {
        activate: active,
        title: 'Overview',
        query: ko.observable('')
    };

    var url = "https://www.googleapis.com/books/v1/volumes?q=the+Cat+In+The+Hat", path = $.getJSON(url);

    path.then(function (data) {
        var books = data.items;
        console.log(books);
        viewModel.model = mapping.fromJS(data.items);
        viewModel.books = ko.computed(function () {
            var search = this.query().toLowerCase();
            return ko.utils.arrayFilter(this.model(), function (book) {
                return book.id().toLowerCase().indexOf(search) >= 0 || book.kind().toLowerCase().indexOf(search) >= 0 || book.etag().toLowerCase().indexOf(search) >= 0
            });
        }, viewModel);
        return viewModel;
    });

});

View:

<section>
    <h2 class="page-title" data-bind="text: title"></h2>
    <header>
        <form action="#">
            <input class=search-query placeholder="Search…" type="search" name="q" data-bind="value: query, valueUpdate: 'keyup'" autocomplete="off" speech x-webkit-speech>
        </form>
    </header>
    <div class="content">
        <ul data-bind="foreach:books">
            <li>
                <strong data-bind="text: id"></strong> –
                <span data-bind="text: kind"></span> –
                <span data-bind="text: etag"></span> -
                <p data-bind="text: volumeInfo.description"></p>
            </li>
        </ul>
    </div>
</section>

There are no error in the console and nothing is displaying on screen, it is just loading


回答1:


define(['plugins/router', 'knockout', 'services/logger', 'durandal/app', 'mapping', 'services/routeconfig', 'services/dataBindingHandlers', 'services/appsecurity'], function (router, ko, logger, app, mapping, routeconfig, dataBindingHandlers, appsecurity) {
    function activate() {
        logger.log(viewModel.title + "'s  View Activated", null, viewModel.title, true);
        return true;
    }

    var viewModel = {
        activate: activate,
        title: 'Overview',
        query: ko.observable(''),
        model: ko.observableArray()
    };

    viewModel.books = ko.computed(function () {
        var search = this.query().toLowerCase();
        return ko.utils.arrayFilter(this.model(), function (book) {
            return book.id().toLowerCase().indexOf(search) >= 0 || book.kind().toLowerCase().indexOf(search) >= 0 || book.etag().toLowerCase().indexOf(search) >= 0;
        });
    }, viewModel);

    var url = "https://www.googleapis.com/books/v1/volumes?q=the+Cat+In+The+Hat", path = $.getJSON(url);

    path.then(function (data) {
        var books = data.items;
        console.log(books);
        viewModel.model(mapping.fromJS(books)());
    });

    return viewModel;
});



回答2:


Move the code to the activate function:

define(['plugins/router', 'knockout', 'services/logger', 'durandal/app', 'mapping', 'services/routeconfig', 'services/dataBindingHandlers', 'services/appsecurity'], function (router, ko, logger, app, mapping, routeconfig, dataBindingHandlers, appsecurity) {
    function activate() {
        logger.log(viewmodel.title + "'s  View Activated", null, viewmodel.title, true);
        var url = "https://www.googleapis.com/books/v1/volumes?q=the+Cat+In+The+Hat", 
        path = $.getJSON(url);
        return path.then(function (data) {
        var books = data.items;
        console.log(books);
        viewModel.model = mapping.fromJS(data.items);
        viewModel.books = ko.computed(function () {
            var search = this.query().toLowerCase();
            return ko.utils.arrayFilter(this.model(), function (book) {
                return book.id().toLowerCase().indexOf(search) >= 0 || book.kind().toLowerCase().indexOf(search) >= 0 || book.etag().toLowerCase().indexOf(search) >= 0
            });
        }, viewModel);
    };

    var viewModel = {
        activate: active,
        title: 'Overview',
        query: ko.observable('')
    };
        return viewModel;
    });

});

In the view, update foreach:filteredItems to: foreach:books



来源:https://stackoverflow.com/questions/22741620/what-am-i-doing-wrong-here-returning-viewmodel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!