【问题标题】:Dynamically call a function on child on attr src in knockout.js在 knockout.js 中的 attr src 上动态调用子函数
【发布时间】:2013-10-11 19:01:38
【问题描述】:

我试图在 attr src 上调用一个函数但失败了。这是我尝试过的。

function FavoriteViewModel() {
    var self = this

    self.FavoriteProfiles   =   ko.observableArray([])

    self.getRating  =   function(Rating){
        //here i want conditions and concat image path
        return does nothing here
    }

    self.LoadData   =   function(){
        //run ajax and put its result in self.FavoriteProfiles
        self.FavoriteProfiles(Result)
    }   

    self.LoadData()
}

当我运行 ajax 时,这会带来这个结果。结果是多个我只发布单个对象以了解

ProfileId   20
Age         21
Gender      "F"
Rating      4

我像这样绑定数据

<div id="favorite-related-profiles" data-bind="foreach:FavoriteProfiles">
<article class="prfl_box">
    <p>
        <span id="favorite-related-age" data-bind="text:Age"></span>               
        <span id="favorite-related-gender" data-bind="text:Gender"></span>               
        <br>
        <img id="favorite-related-rating" class="pro_rating" src="" data-bind="attr:{src:Rating}">
    </p>
</article>
</div>

当我像这样尝试这种绑定时

<img id="favorite-related-rating" class="pro_rating" src="" data-bind="attr:{src:$root.getRating.bind($data,Rating)}">

我在 src 中得到了这个

src="function () { [native code] }"

如何动态生成 src 属性。
注意我需要显示图像。图像被命名为 4rating.png 、 5rating.png 、 default.png。 我想检查 src 属性中的 rating 是否为 4 assing 4rating。我怎样才能做到这一点。

【问题讨论】:

标签: javascript jquery knockout.js


【解决方案1】:

好的,他们的方法很少。现在,如果我理解了您的问题,那么您需要将 src 属性设置为 4rating.png5rating.png 等等,具体取决于评级值分别为 4,5。

如果是这种情况,请查看DEMO - A dirty way

现在,让它按照您的代码运行:-

您可以查看DEMO2- Proper way。如果您检查元素并看到 yopu,您会发现 HTML 标记如下:-

<td data-bind="attr:{src: $root.getRating($data)}" src="4rating.png">
            </td>

希望对你有帮助。

已编辑:-

只是一个建议,当您使用 Knockout Model's 时,您可以将您的模型分开。保持您的流程简单,它将更具可扩展性。 我将分享我如何使用 Revealing Module Pattern DEMO 学习敲除编码。

像这样简单地创建您的视图模型:-

function FavoriteViewModel(data) {
    var self = this

    self.ProfileId = data.ProfileId;//Do some exception handling
    self.Age = data.Age;//Do some exception handling
    self.Gender = data.Gender;//Do some exception handling
    self.Rating = data.Rating;//Do some exception handling
    self.RatingExtended = data.Rating + "rating.png"; //Some random stuff

}

创建一个变量来保存您最喜欢的数组,并将 data-bind 转换为 HTML。

var FavoriteProfiles = ko.observableArray([]);

最后,AJAX 调用获取数据并将其分配给您的FavoriteProfiles

var ajaxdata = DummyAjaxCall(); //get all profiles

        $.each(ajaxdata, function (index, value) {
            FavoriteProfiles.push(new FavoriteViewModel(value)); //Create a Model
        });

【讨论】:

  • 感谢您的回答,它真的很有帮助。在你回答之前我发现了this
  • 您发布的最后一个示例我已经尝试过了。我没有在 Rating Extended 中连接,而是使用了计算的 observable,但它不起作用。
  • @raheelshan 它应该对你有用,只是为了交叉验证Demo3 with Computed Observables :)
  • 谢谢我会试试这个
【解决方案2】:

将您的数据绑定更改为:

data-bind="attr:{src:$root.getRating.bind($data,Rating)}"

所有改变的是我从函数调用中获取了'',因为这样做实际上是将函数的 src 设置为字符串。

编辑:尝试删除函数上的绑定

data-bind="attr:{src:$root.getRating($data,Rating)}"

【讨论】:

  • 谢谢,我已经尝试过了,但那不起作用。但顺便说一句,你测试过吗?它有效吗?
  • 我没有试过你的例子,但这是正确的做法。在调试你的代码时,getRating 函数是否真的被命中了?
猜你喜欢
  • 1970-01-01
  • 2014-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-03
  • 1970-01-01
  • 2013-03-31
  • 1970-01-01
相关资源
最近更新 更多