这不正确的选择(Incorrect this in select)

2019-08-17 22:44发布

我试图用打字稿与迪朗达尔。 我试图使起动机例如与打字稿,这适用于大多数的方法和类的工作。 然而,在Flickr的类下面我体验在选择方法的问题。 当这个方法被调用似乎这不是Flickr的类,但所选择的项目。 有人可以帮助我弄清楚什么是错的? 其他的方法是否按预期工作。

亲切的问候,Marwijn

///<reference path='../../Scripts/typings/requirejs/require.d.ts'/>
///<reference path='../../Scripts/typings/durandal/durandal.d.ts'/>
///<reference path='../../Scripts/typings/knockout/knockout.d.ts'/>

class Flickr 
{
    app: App;
    http: Http;

    displayName = 'Flickr';
    images = ko.observableArray([]);

    constructor(app: App, http: Http)
    {
        this.app = app;
        this.http = http;
    }

    public activate() : any 
    {
        //the router's activator calls this function and waits for it to complete before proceding
        if (this.images().length > 0) 
        {
            return;
        }

        return this.http.jsonp('http://api.flickr.com/services/feeds/photos_public.gne', { tags: 'mount ranier', tagmode: 'any', format: 'json' }, 'jsoncallback').then((response)=>
        {
            this.images(response.items);
        });
    }
    public select(item : any) {
        //the app model allows easy display of modal dialogs by passing a view model
        //views are usually located by convention, but you an specify it as well with viewUrl
        item.viewUrl = 'views/detail';
        this.app.showModal(item);
    }
    public canDeactivate() : any
    {
        //the router's activator calls this function to see if it can leave the screen
        return this.app.showMessage('Are you sure you want to leave this page?', 'Navigate', ['Yes', 'No']);
    }
}

define(['durandal/http', 'durandal/app'], function (http, app) 
{
    return new Flickr(app, http);
} );

编译为以下JavaScript:

var Flickr = (function () {
    function Flickr(app, http) {
        this.displayName = 'Flickr';
        this.images = ko.observableArray([]);
        this.app = app;
        this.http = http;
    }
    Flickr.prototype.activate = function () {
        var _this = this;
        if(this.images().length > 0) {
            return;
        }
        return this.http.jsonp('http://api.flickr.com/services/feeds/photos_public.gne', {
            tags: 'mount ranier',
            tagmode: 'any',
            format: 'json'
        }, 'jsoncallback').then(function (response) {
            _this.images(response.items);
        });
    };
    Flickr.prototype.select = function (item) {
        item.viewUrl = 'views/detail';
        this.app.showModal(item);
    };
    Flickr.prototype.canDeactivate = function () {
        return this.app.showMessage('Are you sure you want to leave this page?', 'Navigate', [
            'Yes', 
            'No'
        ]);
    };
    return Flickr;
})();
define([
    'durandal/http', 
    'durandal/app'
], function (http, app) {
    return new Flickr(app, http);
});
//@ sourceMappingURL=flickr.js.map

Answer 1:

为了得到“这个”引用你可以做以下(假设flickr.html观点没有改变)视图模型:

更改点击缩略图从一个标签绑定

<a href="#" class="thumbnail" data-bind="click:$parent.select">

<a href="#" class="thumbnail" data-bind="click: function (item) { $parent.select(item); }">

因为你是那么直接调用$父对象上的选择方法(视图模型)视图模型将被绑定到“本”。



文章来源: Incorrect this in select