Giter VIP home page Giter VIP logo

knockout.observabledictionary's Introduction

To use the knockout observable dictionary you simply need to reference knockout and then this

<script type='text/javascript' src="http://github.com/downloads/SteveSanderson/knockout/knockout-2.0.0.js"></script>
<script type='text/javascript' src="https://raw.github.com/jamesfoster/knockout.observableDictionary/master/ko.observableDictionary.js"></script>

Then pass a javascript object to ko.observableDictionary() as follows.

var person = {
    name: 'Joe Bloggs',
    height: 180,
    'hair colour': 'brown'
};

var viewModel = {
    person: new ko.observableDictionary(person)
};

ko.applyBindings(viewModel);

To loop over the items in the dictionary simply data bind to the items property. Each item has a key property and a value property

<ul data-bind="foreach: person.items">
    <li>
        <span data-bind="key"></span>
        <span data-bind="value"></span>
    </li>
</ul>

You can also data bind to specific elements within the dictionary using the method get

<label>Name: <input data-bind="person.get('name')" /></label>

You can even data bind to elements which don't exist yet. In this case, if you update the value it will add a new item to the dictionary.

<label>Company: <input data-bind="person.get('company')" /></label>

To set a value on the dictionary in code use either:

  • the set method: viewModel.person.set('hair colour', 'blue');. or
  • the get method: viewModel.person.get('hair colour')('blue');.

The obersvableArray methods indexOf, remove, sort and push have also been overridden to behave as expected with dictionaries e.g. viewModel.person.remove('height') and viewModel.person.indexOf('hair colour')

Enjoy

knockout.observabledictionary's People

Contributors

iwannabeamillionaire avatar jamesfoster avatar jamesfoster-excelpoint avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

knockout.observabledictionary's Issues

Uncaught Error: Unable to parse bindings

I used the latest file in HEAD.

Setup the bindings as follows (please ignore the irrelevant bits):

var viewModel = {
    fills : ko.observableArray(),
    symbols : {MSFT:0},
    symbol_totals : new ko.observableDictionary({}),
    addFill : function(fill){ 
        var sym = fill[55];
        var total = (sym in this.symbols) ? this.symbols[sym] : 0;
        total = total + new Number(fill['rate']);
        this.symbols[sym] = total;

        this.symbol_totals.set(sym,total);

        this.fills.push(fill);
    }
};

ko.applyBindings(viewModel);

I bind them as such:

<table id="symbol-totals" class="zebra-striped condensed-table">
   <thead>
       <tr><th>Symbol</th><th>Total Commission</th></tr>
   </thead>
   <tbody data-bind="foreach: symbol_totals.items">
       <tr>
           <td data-bind="key"></td>
           <td data-bind="value"></td>
       </tr>
   </tbody>
</table>

When I used the 0.1 version, I saw nothing. I mean no errors, no binding updates (as if I hadn't written any observableDictionary code). With the latest version, I see the following exception. Please note that I am updating data whenever websocket send me an update.

I am testing this on the latest version of chrome, on a windows 64 bit machine. I'm using knockout version 2.0.0.


knockout-2.0.0.js:6Uncaught Error: Unable to parse bindings.
Message: SyntaxError: Unexpected token };
Bindings value: key

How to load with requirejs?

My project uses requirejs to manage dependencies, and when I set up the config for knockout and then observableDictionary, I get a "ko is not defined" error, I think because your code is expecting ko to be in the global scope. My config looks like this:

requirejs.config({
    paths: {
        'knockout': '../Scripts/knockout-2.3.0',
        'observableDictionary': '../Scripts/observableDictionary'
    },
    shim: {
        'observableDictionary': {
            deps: ['knockout'],
            exports: 'observableDictionary'
        }
    }
});

The only solution I have at the moment is to wrap your code in a define block, but that's obviously not a good long-term solution. I'm pretty new to requirejs so it's entirely possible that I'm missing something. Any insight you might have would be great. Thanks!

Does not update the UI when changed

Simple code

function getPageContent(localeKey, pageKey, varToPopulate) {
    var contentRequest = {
        LocaleKey: localeKey,
        PageKey: pageKey
    };


    $.getJSON('api/pagecontent', contentRequest, function (data) {
        varToPopulate.pushAll(data.ContentElements);

When I change the locale key I need it to repopulate and have the UI update too. However I don't know how to get that to work (oh, and 'subscribe' would be lovely!)

Add Bower support

I really like your library but it would be nice if it would be available as a Bower package (see http://bower.io/) as it is the de facto standard for JavaScript packages. You just need to setup and configure a "bower.json" file. If you wish then I can help you to set it up! :)

๐Ÿ‘ for your efforts!

Can "value" be an object?

All the examples use simple strings in the value property, But i would like to put a complex object in there.

Then in binding, dot-down to the desired property, or use knockouts inline function to build the display value.

Is this possible? I will try it myself.

Customize ko.toJS for observableDictionary

I recently became a user and a big fan of knockout.observableDictionary library, I'm sorry it wasn't accepted as a part of knockout.

I noticed that observableDictionary has a toJSON customized function, which reassembles the javascript object, before sending it to the server. I would like toJS function to be customized as well, since the standard ko.toJS implementation converts the dictionary into a weird object, that isn't equivalent to the original passed to observableDictionary.

I opened the issue #2490 on knockout repository and someone suggested me to use Proxies.
I think I can't easily adopt this solution, because I should write custom code to handle IE, every time a toJS function is invoked. I would like a more transparent solution.
Do you have any ideas?

Nested observable dictionary

Hi.

I need put to frontend observable dictionary with few nested observable dictionary. So my code looks like

var DevicesViewModel = function(node) {
        var self = this;
        self.node = node;
        self.devices_commands = ko.observableDictionary({});

        self.addDevice = function(device) {
            self.devices_commands.push(device, ko.observableDictionary({}));
        };

        self.parse_cmd = function(cmd) {
            var device_name = cmd["device"];
            if(-1 == self.devices_commands.indexOf(device_name)) { self.addDevice(device_name); }
            self.devices_commands.get(device_name)().set(cmd["cmd_type"], cmd);

            if(cmd["cmd_type"] == "AT^SMONI") { parse_st_smoni(cmd); }
            else if(cmd["cmd_type"] == "AT^MONI") { parse_at_moni(cmd); }
            else if(cmd["cmd_type"] == "AT^SMONP") { parse_at_smonp(cmd); }
        };
    };

This is my temaplete

<div id="devices" class="row" data-bind="foreach: devices_commands.items">
    <div class="col-xs-12 col-sm-12 col-md-6 col-md-offset6" data-bind="attr: {id: key}">
        <div class="">
            <h3 data-bind="text: key"></h3>
            <div data-bind="with: value">
                <div data-bind="with: $parent.get('AT^MONI')">
                    <div data-bind="text: 'cmd_response'"></div>
                </div>
            </div>
            <div data-bind="text: ko.toJSON(value)">
            </div>
        </div>
    </div>
</div>

And data printed in last div

{"ATI":{"timestamp":1496913515.8213873,"cmd_type":"ATI","device":"dUsb","cmd_response":["Cinterion","ALAS3-W","REVISION 00.002"]},"AT^MONI":{"timestamp":1496913516.244922,"cmd_type":"AT^MONI","device":"dUsb","cmd_response":{"SRxLev":"48","EC/n0":"-7.0","connection_status":"No connection","PSC":"12","SQual":"22","CSGid":"--","MCC":"260","UARFCN":"2963","LAC":"A044","MNC":"01","cell":"0C82A45","RSCP":"-66","dedicated_channel":{"ComMod":null,"HSUPA":null,"EC/n0":null,"PhysCh":null,"HSDPA":null,"Slot":null,"SF":null,"RSCP":null},"Mode":"FDD"}},"AT^SMONI":{"timestamp":1496913516.8948753,"cmd_type":"AT^SMONI","device":"dUsb","cmd_response":["3G","FDD","2963","12","-6.5","-65","260","01","A044","0C82A45","23","49","--","NOCONN"]},"AT^SMONP":{"timestamp":1496913517.108912,"cmd_type":"AT^SMONP","device":"dUsb","cmd_response":{"4G":[["1300","-17.0","-123","5","132","0"],["1300","-17.0","-127","1","291","0"]],"2G":[["41","----","-","-","----","------"],["3","----","-","-","----","------"],["14","----","-","-","----","------"],["4","----","-","-","----","------"],["12","----","-","-","----","------"],["40","----","-","-","----","------"]],"3G":[["2963","12","-6.5","-75","-","-","AS","-1"],["2963","121","-24.0","83","-","-","SN","-32768"],["10737","152","-15.0","-84","-","-3","SN","-32768"],["10737","0","-20.0","-90","-","-7","SN","-32768"],["10762","0","-14.0","-84","-","-3","SN","-32768"],["10762","152","-24.0","-93","-","-7","SN","-32768"],["10762","112","-24.0","-94","-","-7","SN","-32768"]]}},"AT^SIND?":{"timestamp":1496913512.909214,"cmd_type":"AT^SIND?","device":"dUsb","cmd_response":[["signal","0","99"],["service","0","1"],["sounder","0","0"],["message","0","0"],["roam","0","0"],["smsfull","0","0"],["audio","0","0"],["simstatus","1","5"],["simdata","0"],["eons","0","3","\"0050006C00750073\"","\"\"","0"],["nitz","0","\"00310037002F00300036002F00300037002C00300035003A00340038003A00320039\"","+08","1"],["psinfo","0","6"],["simlocal","0","1","0"],["lsta","0","0"],["pacsp","0","99"],["steerroam","0"],["pagingcoor","0","1"],["ceer","0","0"],["iccid","0","\"8948011714275163850\""],["euiccid","0","\"\""],["imsi","0","\"260011701124460\""],["ltebot","0","0","0","\"\""],["prov","1","1","\"fallback*\""],["simread","0","0","7"]]},"AT+CEER":{"timestamp":1496913513.3323739,"cmd_type":"AT+CEER","device":"dUsb","cmd_response":["EMM attach failed"]},"AT+GSN":{"timestamp":1496913515.610146,"cmd_type":"AT+GSN","device":"dUsb","cmd_response":"004401081597557"},"AT+COPS?":{"timestamp":1496913517.7437313,"cmd_type":"AT+COPS?","device":"dUsb","cmd_response":["0","0","\"0050006C00750073\"","2"]}}

I want create panel for every command, with timestamp and data from cmd_response variable. As you see there is a lot of type this data. Sometimes this is list, sometimes dictionary.

Can anyone tell me how I should get this nested data?

Best regards.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.