Making count dynamic in list

edited May 2013 in Enyo 2
enyo.kind({
	name: "enyo.sample.ListBasicSample",
	classes: "list-sample enyo-fit",
	components: [
		{name: "list", kind: "PulldownList", count: 1, multiSelect: false, classes: "enyo-fit list-sample-list", onSetupItem: "setupItem", components: [
			{name: "item", classes: "list-sample-item enyo-border-box", components: [
				{name: "index", classes: "list-sample-index"},
				{name: "name"}
			]}
		]}
	],
	names: ["ts"],
	setupItem: function(inSender, inEvent) {
		// this is the row we're setting up
		var i = inEvent.index;
		// make some mock data if we have none for this row
		
		var n = this.names;
		var ni = ("00000000" + i).slice(-7);
		// apply selection style if inSender (the list) indicates that this row is selected.
		this.$.item.addRemoveClass("list-sample-selected", inSender.isSelected(i));
		this.$.name.setContent(n);
		this.$.index.setContent(ni);
	}
});
In the code above, i would want to make the count dynamic based on the names length..is it possible?

Comments

  • You can just call this.$.list.setCount(this.names.length). You'll want to put that somewhere out of the setupItem function.
  • Yes. You would do this.$.list.set("count", this.names.length);
  • edited May 2013
    At all sorry...I have just started with enyo..
    enyo.kind({
    	name: "enyo.sample.ListBasicSample",
    	classes: "list-sample enyo-fit",
    	components: [
    		{name: "list", kind: "PulldownList", count: 0, multiSelect: false, classes: "enyo-fit list-sample-list", onSetupItem: "setupItem",name:"myList",components: [
    			{name: "item", classes: "list-sample-item enyo-border-box", components: [
    				{name: "index", classes: "list-sample-index"},
    				{name: "name"}
    			]}
    		]}
    	],
    	
    	names: {"employees": [{ "id":"John" , "content":"Test" },{ "id":"Madhu" , "content":"Madhu1" }]},
    	
    	setupItem: function(inSender, inEvent) {
    		this.$.list.set("count", this.names.length);
    		// this is the row we're setting up
    		var i = inEvent.index;
    		// make some mock data if we have none for this row
    		
    		var n = this.names.employees[i].id
    		var ni = ("00000000" + i).slice(-7);
    		// apply selection style if inSender (the list) indicates that this row is selected.
    		this.$.item.addRemoveClass("list-sample-selected", inSender.isSelected(i));
    		this.$.name.setContent(n);
    		this.$.index.setContent(ni);
    	}
    });
    Can you please show me where to put it..have put the code not sure if its working
  • You don't want to include that call inside of your setupItem call because that's an event handler for onSetupItem on the List which is how the List communicates down to your app that it needs to set up the item's content (and it provides you with the bits and pieces to do so)

    You can set it, for example, in a response handler for an ajax request. Or you can just call it in an overridden create() method, which is called when the kind is created (in your example, when you would call "new enyo.Sample.ListBasicSample()"):
    create: function() {
        // this is absolutely vital - it calls the create() method of the kind we're inheriting from
        this.inherited(arguments);
        // if your this.names array exists at this point, now is a good time to set the count
        this.$.list.setCount(this.names.length);
    }
    Note that if you're updating the count property after it's been rendered, you also need to call refresh() on the list so it knows that it should refresh the list items:
    this.$.list.setCount(this.names.length);
    this.$.list.refresh();
  • 2 things:

    Get used to using my form: this.$.thing.set("foo", bar); This will be the core way properties are updated in 2.3 (if you're using 2.2, you need to use the other form still)

    I don't think you have to call refresh() (I never seem to have to, I think @arthur is smoking crack in his off time)
  • @sugardave : Get used to using my form: this.$.thing.set("foo", bar); This will be the core way properties are updated in 2.3 (if you're using 2.2, you need to use the other form still)
    Is the old way still going to be supported?
  • yes, it will be. However, only for published properties. You can use set/get for any property, and that feeds into the new observation/binding/computed property system.
Sign In or Register to comment.