Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7795907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:12:25+00:00 2026-06-01T23:12:25+00:00

I have created a simple content rotator using the excellent Cycle.js plugin, this gets

  • 0

I have created a simple content rotator using the excellent Cycle.js plugin, this gets it’s content from my Knockout.JS view model property (appViewModel.mediaPlayer in this case):

e.g.

  var testData = [{ url: "media/1.png" }, { url: "media/2.jpg"}];

    var appViewModel =
    {
        mediaPlayer: new ko.observableArray(testData)
    };

    ko.applyBindings(appViewModel);

This is rendered in the view using JQuery Tmpl e.g.

 <script id="mediaPlayerTemplate" type="text/template">
        <img src=${url} />
    </script>

<div class="adContainer" data-bind="template: {name: 'mediaPlayerTemplate', foreach: mediaPlayer}">
    </div>

To start the image transition I simply call the Cycle plugins method “cycle”:

 $('.adContainer').cycle({
            fx: 'fade',
            timeout: 1000,
            speed: 500
        });

This works great, however when I update my view model media player content the cycle plugin stops working…this is not a problem as a simple call to cycle() will start it again.

However, my question is, where is the best place to make the call to cycle() to update the view? I figured I could subscribe to the mediaPlayer changes, simply calling the method when required however this would mean that I have to put the JQuery element/view logic in the View Model which feels wrong (perhaps I’m trying to be too purist!).

In short, how do I trigger functions in the view from the view model without the view model knowing about the views function? In Silverlight/WPF this was possible with triggers in XAML but I’m not sure how to achieve the same separation using Knockout.JS

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T23:12:27+00:00Added an answer on June 1, 2026 at 11:12 pm

    I think it is better to do this using bindinghandlers, this will also allow you properly dispose plugin when content is re-rendered.

    Your example (it will change cycle after 5 seconds):

    http://jsfiddle.net/nickolsky/uXDUA/2/

    HTML:

    <script id="mediaPlayerTemplate" type="text/template">
            <img src=${url} />
        </script>
    
    <div data-bind="template: {name: 'mediaPlayerTemplate', foreach: mediaPlayer}, cycle: { fx: 'fade', timeout: 1000, speed: 500 }, cycleLinked: mediaPlayer">
        </div>
    

    ​

    JAVASCRIPT:

    ko.bindingHandlers.cycle = {
        init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
            $element = $(element);
            var options = ko.utils.unwrapObservable(valueAccessor()) || {};
            //console.log('cycle_init');
            var _starting = false;
    
            var _cycle = function() {
                if(_starting) return;
                _starting = true;
                //console.log('cycle_create');
                $element.hide();
                setTimeout(function() { $(element).cycle(options); $element.show(); _starting = false; }, 0);
            };
    
            var subscription = allBindingsAccessor().cycleLinked.subscribe(_cycle);
            //handle disposal
            ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
                //console.log('cycle_destroy');
                $element.cycle("destroy");
                subscription.dispose();
            });
            _cycle();
    
        }
    };
    
    
    var testData = [{
        url: "http://cloud.github.com/downloads/malsup/cycle/beach1.jpg"},
    {
        url: "http://cloud.github.com/downloads/malsup/cycle/beach2.jpg"}];
    
    var appViewModel = {
        mediaPlayer: new ko.observableArray(testData)
    };
    
    ko.applyBindings(appViewModel);
    
    setTimeout(function() {
     appViewModel.mediaPlayer.push({url: "http://cloud.github.com/downloads/malsup/cycle/beach3.jpg"});
    appViewModel.mediaPlayer.push({url: "http://cloud.github.com/downloads/malsup/cycle/beach4.jpg"});
        }, 5000);​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could someone help me on this, I have created simple web services using axis2
i have created a simple custom module to store title, date and content from
I have created a simple WordPress plugin that automatically sets my new sites up
I have created a simple web browser using c# and I want to block
I have created a simple Java class as follows: I pass content as a
I'm using Rails3 and have created a simple document table with a document_name (string)
I have created a rails application that has a simple RESTful json API. This
I have created a simple Google chrome extension that overrides CSS classes using the
I have created a Gallery View with 2 Views with 3 simple TextViews to
How to manually create Friendly URLs? (PHP) So I have created simple php file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.