2012-04-26 56 views
0

我想檢查'類型'綁定,如果他等於某個字符串,但看起來他沒有執行它。Knockout.js如果語句問題

我的HTML頁面:

<div class="socialWrapper" data-bind="foreach: facebookposts"> 
     <!-- ko if: {Type()==='link'}--> 
     <img data-bind='attr: { src: FromPicture }'/> 
     <p data-bind="text:From"></p> 
     <!-- /ko --> 
</div> 

當我剛剛返回:

<p data-bind="text: Type"></p> 

我的輸出= 「字符串」

正如你可以看到我想要得到基於正確觀測if語句。 視圖模型:

function Post(allData) { 
    var profileImageUrl = "http://graph.facebook.com/" + allData.from.id + "/picture?type=large"; 
    this.Type = ko.observable(allData.type); 
    this.From = ko.observable(allData.from.name); 
    this.FromPicture = ko.observable(profileImageUrl); 
    this.Created = ko.observable(allData.created_time); 
    this.Comments = ko.observable(allData.comments.count); 
    this.Message = ""; 
    this.Likes = ""; 
    this.LinkImage = ""; 
    this.LinkUrl = ""; 
    this.LinkName = ""; 
    this.LinkTitle = ""; 
    this.LinkDescription = ""; 
    this.Story = ""; 
    this.Photo = ""; 
    this.PhotoDescription = ""; 


    if (allData.type === 'status') { 
     this.Message = ko.observable(allData.message); 
     this.Likes = ko.observable(allData.likes); 
    } 
    if (allData.type === 'link') { 
     this.Message = ko.observable(allData.message); 
     this.LinkImage = ko.observable(allData.picture); 
     this.LinkUrl = ko.observable(allData.link); 
     this.LinkName = ko.observable(allData.name); 
     this.LinkTitle = ko.observable(allData.caption); 
     this.LinkDescription = ko.observable(allData.description); 
     this.Likes = ko.observable(allData.likes); 
    } 
    if (allData.type === 'photo') { 
     this.Story = ko.observable(allData.story); 
     this.Photo = ko.observable(allData.picture); 
     this.PhotoDescription = ko.observable(allData.description); 
    } 
} 

    var masterViewModel = { 
     facebookposts: ko.observableArray([]), 
     getFacebookObjects: function() { 
      var getUrl = '/api/[email protected]["fb_access_token"]'; 
      $.ajax({ 
       url: getUrl, 
       type: 'GET', 
       dataType: 'json', 
       success: function (allData) { 
        var mappedPosts = $.map(allData, function (item) { 
         return new Post(item); 
        }); 
        masterViewModel.facebookposts(mappedPosts); 
       }, 
       statuscode: { 
        401: function() { 
         console.log("Not Authorized"); 
        } 
       } 
      }); 
     } 
    }; 

$(document).ready(function() { 
    ko.applyBindings(masterViewModel); 
    masterViewModel.getFacebookObjects(); 
}); 

任何人有關於if語句如何解決這一問題的想法?

+0

如果不工作,你的意思是它似乎始終評估爲真?總是假?或者它給你一些錯誤? – 2012-04-26 14:09:10

+0

另外,試試Type()==='link'。可觀察對象實際上是函數,您需要使用括號來調用函數並實際獲取值。 – 2012-04-26 14:10:16

+0

@Matt Burland:它總是評估爲false,並且Type()==='link'不起作用。 – 2012-04-26 14:12:30

回答

2

修訂 - 與全視圖模型

我會做,看起來像這樣

function Post(allData) { 
    var profileImageUrl = "http://graph.facebook.com/" + allData.from.id + "/picture?type=large"; 
    this.Type = ko.observable(allData.type); 
    this.From = ko.observable(allData.from.name); 
    this.FromPicture = ko.observable(profileImageUrl); 
    this.Created = ko.observable(allData.created_time); 
    this.Comments = ko.observable(allData.comments.count); 
    this.Message = ""; 
    this.Likes = ""; 
    this.LinkImage = ""; 
    this.LinkUrl = ""; 
    this.LinkName = ""; 
    this.LinkTitle = ""; 
    this.LinkDescription = ""; 
    this.Story = ""; 
    this.Photo = ""; 
    this.PhotoDescription = ""; 

    var self = this; 
    this.isLink = ko.computed(function() { 
     return self.Type() === 'link'; 
    }); 

    if (allData.type === 'status') { 
     this.Message = ko.observable(allData.message); 
     this.Likes = ko.observable(allData.likes); 
    } 
    if (allData.type === 'link') { 
     this.Message = ko.observable(allData.message); 
     this.LinkImage = ko.observable(allData.picture); 
     this.LinkUrl = ko.observable(allData.link); 
     this.LinkName = ko.observable(allData.name); 
     this.LinkTitle = ko.observable(allData.caption); 
     this.LinkDescription = ko.observable(allData.description); 
     this.Likes = ko.observable(allData.likes); 
    } 
    if (allData.type === 'photo') { 
     this.Story = ko.observable(allData.story); 
     this.Photo = ko.observable(allData.picture); 
     this.PhotoDescription = ko.observable(allData.description); 
    } 
} 

你的視圖模型一個計算觀察到的那麼

<div class="socialWrapper" data-bind="foreach: facebookposts"> 
     <!-- ko if: isLink --> 
     <img data-bind='attr: { src: FromPicture }'/> 
     <p data-bind="text:From"></p> 
     <!-- /ko --> 
</div> 

個人而言,我喜歡讓我視圖儘可能乾淨,這意味着沒有JavaScript評估。更好的分離問題。

+0

我更新了你的整個視圖模型 – 2012-04-26 14:45:30

+0

是的,做了這份工作,謝謝! – 2012-04-26 14:56:00

+0

我忘了()當我第一次嘗試它:) – 2012-04-26 14:58:03

0

我認爲你需要使用$父上下文綁定。嘗試了這一點 -

<div class="socialWrapper" data-bind="foreach: facebookposts"> 
    <!-- ko if: $parent.Type === 'link'--> 
    <img data-bind='attr: { src: FromPicture }'/> 
    <p data-bind="text:From"></p> 
    <!-- /ko --> 
</div> 

http://jsfiddle.net/belthasar/DaN2X/

+0

如果我嘗試獲取錯誤:Uncaught錯誤:無法解析綁定。 消息:SyntaxError:意外的令牌。 綁定值:if:{$ parent.Type()==='link'}。我也沒有括號{}。 – 2012-04-26 14:35:12

+0

我在IE或FF中看不到錯誤。你能提供一個facebookposts結構的例子嗎? – 2012-04-26 14:42:15

+0

我用Chrome來測試它。但它現在已經修復,感謝您的幫助! – 2012-04-26 14:58:50