2013-03-24 73 views
2

我正在嘗試使用Durandal打字稿。我正在嘗試使用打字稿工作,這對大多數方法和類都有效。但是在下面的Flickr類中,我在select方法中遇到了一個問題。當這個方法被調用時,似乎這不是Flickr類,而是選定的項目。 有人可以幫我弄清楚什麼是錯的? 其他方法按預期工作。編譯爲以下JavaScript錯誤在選擇

親切的問候, 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); 
}); 

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 

回答

4

得到 '這個' 引用,你可以做以下視圖模型(假設的Flickr .html查看未更改):

更改縮略圖標籤上的點擊綁定

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

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

因爲你然後直接調用視圖模型將被綁定到「這個」的$父對象(視圖模型)上的選擇方法。

+0

+1有趣的一點,我總是想知道爲什麼人們這樣做。它現在有意義:) – 2013-03-25 15:11:55

+0

我目前在工作,所以我不能訪問該項目,但今天晚上會嘗試。在此期間,我發現這個:http://stackoverflow.com/questions/13277537/typescript-and-knockout-binding-to-this-issue-lambda-function-needed。我想知道它是否是同樣的問題。我喜歡Markus解決方案,因爲它不需要更改視圖或viewModel,因爲Rebind可能在代碼中的一個位置完成。 – Marwijn 2013-03-25 15:18:09

+0

如果你想要導航,那麼你可能想要返回函數的值,例如「click:function(item){return $ root.openDocument(item);}」我需要使用Pager.js來做到這一點 – chrisb 2013-04-12 17:09:16