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 6563619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:52:19+00:00 2026-05-25T13:52:19+00:00

So, I have observable array with sites, which is shown via template. If I’ll

  • 0

So, I have observable array with sites, which is shown via template. If I’ll add site to this array, template is not updated, but if I’ll remove site from array – voila! template became updated and all previously added sites became displayed too.

If I’ll use nifty hack (commented in code) with replacement of whole array to new one then everything works.

BTW, I load template via AJAX and use “ko.applyBindings(viewModel)” after. I assume that works fine, because initial sites are displayed correctly.

$(function(){

//site entry in user's sites list
var siteObject = function(url, lastChecked, status){
    this.url = url;
    this.lastChecked = (lastChecked == 'undefined') ? '' : lastChecked;
    this.status = (status == 'undefined') ? 'not_checked_yet' : status;
    this.toDelete = false;
    this.remove = function() {viewModel.sites.remove(this)};
};

viewModel = {
    //=========== sites list managment ==========================
    sites: ko.observableArray(),
    //on "add" click in "add site" form
    addSite: function(){
        var $form = $('#add_site_form');
        var siteUrl = $form.find('input[name="site"]').val();

        /*nifty hack  <----
        var sites = this.sites();
        sites.push(new siteObject(siteUrl));
        this.sites(sites);*/

        this.sites.push(new siteObject(siteUrl));
    },
    //on "remove sites" button click
    removeSites: function() {
        var sitesToRemove = [];
        $.each(this.sites(), function(){
            if (this.toDelete) sitesToRemove.push(this);
        });
        if (sitesToRemove.length == 0)
            alert("Ни одного сайта не было выбрано для удаления.");
        else {
            var message = "Вы точно хотите перестать отслеживать";
            for (var i in sitesToRemove) {
                message += "\n\"" +  sitesToRemove[i].url + "\"";
            }
            message += "?";
            if (confirm(message)) {
                $.each(sitesToRemove, function(){this.remove()});
                //save new sites list to db
                this.saveSitesListToDb();
            }
        }   
        //hide form
        $('#remove_sites_form').slideToggle();
        //toggle checkboxes
        $('#content_sites_list .site_info input[type="checkbox"]').slideToggle();
    };

And the template:

<!-- end of menu -->
<div id="content_sites_list" 
     class="grid_12" 
     data-bind="template: {name: 'sites_list_template', foreach: sites}"></div>

  <!-- Templates -->

  <script id="sites_list_template" type="text/x-jquery-tmpl">
    <div class="site">
        <div class="site_panel grid_12">
            <div class="site_info">
                &ndash;
                <input type="checkbox" value="${url}" 
                       class="delete_checkbox" data-bind="checked: toDelete" /> 
                <a href="${url}" class="show_site_stat">${url.substr(7)}</a>
                {{if status == "200"}}
                    <img src="img/green_light.png" alt="ok"/>
                {{/if}}             
            </div>
            <div class="site_stat">
                <div class="site_last_check">Последняя проверка: ${dateTimestamp}</div>
            </div>
        </div>
    </div>
  </script>

I’ve tried this on latest beta on knockoutjs and on stable one.

  • 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-05-25T13:52:20+00:00Added an answer on May 25, 2026 at 1:52 pm

    I have made a jsFiddle which works fine.

    There were some problems that JSLint was complaining about in the removeSites function of the viewModel. I fixed those and added a button and input field to be able to give some input, and everything ran smooth.

    So you could try updating your removeSites function and see if it helps you,

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a an array of observable collections and I want to display all
You have a pull-oriented Observable/Listenable which notifies Observers/Listeners when some state changes. The state
I have made a generic Observer interface and an Observable class, but can't compile
I have this problem when i try to synchronize a observable list with listbox/view
i have function, which has to accept two types of data - Observable collection
I have the following view model which contains an array of elements function ReservationsViewModel()
If I have an observable array foos = [{ name: a }, { name:
What is the best way to handle an observable array which can be committed/thrown
I have a listbox bound to a view model observable collection: This works fine,
I'm trying to add a new item to an observable array when a button

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.