dongque1646 2014-09-25 20:37
浏览 75
已采纳

从外部视图模型中调用knockout.js中的函数

I'm using CakePHP 2.3.8 and I'm trying to call a function from within a knockout.js view model, but I'm having some trouble understanding what's going on.

If a specific variable is set (in PHP), then I want to display a div but I'm having trouble getting it to work. When I call it from the PHP code, the div doesn't display, however an alert message from within the method triggers so I know that code is being reached.

<div data-bind = "visible: someDiv">
    I'm visible
</div>

Here's the knockout viewmodel

function myViewModel(){

    var self = this;

    self.someDiv = ko.observable(false); //the div starts out hidden

    self.editing = ko.observable(false);

    //if the editing variable is changed, display someDiv
    self.editing.subscribe(function(){
        alert('edit changed'); //this alert triggers every time, even from the PHP call
        self.someDiv(true);  //someDiv only shows if I call from within the view model
    });

    //if I manually change editing to true from within the viewmodel, the div shows
    //self.editing(true);

}
ko.applyBindings(new myViewModel());

Here's the PHP code that initiates the sequence of events

echo $this->Html->script('knockout-2.3.0');
echo $this->Html->script('viewmodel');

//if the edit variable is set, the "someDiv" should be set to true
if(isset($edit)){
    <script>
        window.vm = new myViewModel();
        window.vm.editing(true); //this will trigger the alert from the subscribe function, but the div doesn't display
    </script>
}

Why is it when I change the editing value to true from PHP the div doesn't display, but if I change it from within the viewmodel it displays?

Is it even possible to do what I'm attempting?

Edit

I am applying bindings to my viewmodel in that JS file. I am not applying bindings again in the PHP file.

What I mean from "variable is set (in PHP)" is that the source of the data originates from PHP, although it is JavaScript setting the value in the end. I kept it short for my example above, so really it would be like this (not that it makes much of a difference)

//if the edit variable is set, the "someDiv" should be set to true
if(isset($edit)){
    <script>
        window.vm = new myViewModel();
        window.vm.editing(<?php echo $edit; ?>); //this will trigger the alert from the subscribe function, but the div doesn't display
        window.vm.another(<?php echo $something_else_here; ?>);
    </script>
}
  • 写回答

1条回答 默认 最新

  • douji6461 2014-09-25 21:54
    关注

    Why is it when I change the editing value to true from PHP the div doesn't display, but if I change it from within the viewmodel it displays?

    I don't see anywhere that you are setting it 'from php' it seems you are setting the value from JavaScript. If window.vm.editing(true); is firing the subscribe function then it is working successfully. The real question at that point is are you sure that you only have one instance of the VM that is being bound to the DOM? I don't see anywhere in your code where you are applying bindings at all so could you show that code as well?

    I suspect your VM actually looks something like this -

    function viewModel() {
        // some code
    }
    
    ko.applyBindings(new viewModel());
    

    Or is being initialized twice. Remember that you need to reference the same instance of your view model.

    window.vm = new myViewModel();
    ko.applyBindings(window.vm);
    
    //if the edit variable is set, the "someDiv" should be set to true
    if(isset($edit)){
        <script>
            window.vm.editing(true); //this will trigger the alert from the subscribe function, but the div doesn't display
        </script>
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R