1

我是 Sencha Touch 2 的 MVC 结构的新手。

在我看来,我有一个标签如下:

{ xtype: 'label', itemId: 'title', html: 'title' }

在我的控制器中,如何设置此标签的值?

目前我的控制器(处理教程示例):

Ext.define("NotesApp.controller.Notes", {

extend: "Ext.app.Controller",
config: {
    refs: {
        // We're going to lookup our views by xtype.
        noteView: "noteview",
        noteEditorView: "noteeditorview",
        notesList: "#notesList"
    },
    control: {
        noteView: {
            // The commands fired by the notes list container.
            noteNextCommand: "onNoteNextCommand",
            noteAnswerCommand: "onNoteAnswerCommand"
        },
        noteEditorView: {
            // The commands fired by the note editor.
            saveNoteCommand: "onSaveNoteCommand",
            deleteNoteCommand: "onDeleteNoteCommand",
            backToHomeCommand: "onBackToHomeCommand"
        }

    }
},

onNoteNextCommand: function () {
       var noteView = this.getNoteView();           
       console.log("loaded view"); 
       //set label here                  
},
// Base Class functions.
launch: function () {
    this.callParent(arguments);
    var notesStore = Ext.getStore("Notes");
    notesStore.load();
    console.log("launch");
},
init: function () {
    this.callParent(arguments);
    console.log("init");
} });

完整的查看代码:

Ext.define("NotesApp.view.Note", { extend: "Ext.Container", 别名: "widget.noteview",

config: {
    layout: {
        type: 'fit'
    },
    items: [
            {
            xtype: "toolbar",
            title: "Random Question",
            docked: "top",
            items: [
                    { xtype: 'spacer' },
                    {
                        xtype: "button",
                        text: 'List',
                        ui: 'action',
                        itemId: "list"
                    }
                    ]
            },
            {                
                    xtype: "label",
                    html: 'question',                        
                    itemId: "question"                        
            },
            {                
                    xtype: "label",
                    html: 'answer',                        
                    itemId: "answer"                        
            }
            ],
    listeners: [{
                delegate: "#list",
                event: "tap",
                fn: "onListTap"
                }, 
                {
                delegate: "#question",
                event: "tap",
                fn: "onQuestionTap"
                },
                {
                delegate: "#answer",
                event: "tap",
                fn: "onAnswerTap"
                }]
},    
onListTap: function () {
    console.log("list");
    this.fireEvent("showList", this);
},
onQuestionTap: function () {
    console.log("noteAnswer");
    this.fireEvent('noteAnswer', this);
},
onAnswerTap: function () {
    console.log("noteNext");
    this.fireEvent('noteNext', this);
} });
4

2 回答 2

2

您需要在控制器中添加对标签的引用:

config: {
  refs: {
    yourlabel : #YOUR_LABEL_ID'
  }
  …
}

然后在您的控制器中,您可以通过调用访问标签this.getYourlabel();

所以,为了改变标题,你需要做(在你的控制器中的任何地方)

this.getYourlabel().setHtml('Label');

希望这可以帮助

于 2012-05-21T11:26:30.493 回答
2

给你的标签一些id属性值

{                
  xtype: "label",
  html: 'answer',                        
  id: "answerLabel"                        
}

然后在你的controller.

.....
..... // Controller code ..
refs: {
     answerLabel: '#answerLabel',
    },
control: {
     answerLabel: {
       tap: 'answerLabelFn'
     }
}
.....
.....
answerLabelFn : function() {
      // Your Label tap handler code...
}
于 2012-05-21T04:36:51.353 回答