Титановые мобильные Javascript объекты - PullRequest
1 голос
/ 07 ноября 2011

Хорошо, я новичок в Titanium, и я в значительной степени новичок в Javascript

Я пытался сделать это:

app.view.newMatrix = function() {
return {
    matrix = Titanium.UI.createWindow({  
        title:'Add a New Matrix',
        backgroundColor:'stripped',
        navBarHidden: false
    }),     
    // navbar buttons
    cancel = Titanium.UI.createButton({
        title:'Cancel'
    }),
    save = Titanium.UI.createButton({
        title:'Save',
        style:Titanium.UI.iPhone.SystemButton.SAVE
    }),
    name_label = Titanium.UI.createLabel({
        text: "Matrix Name:",
        font: { fontsize: 12, fontstyle: 'italic', color: '#336699' },
        height: 35,
        top: 35,
        left: 30,
        width: 150,
        color: "black"
    }),
    name = Titanium.UI.createTextArea({
        color: '#336699',
        height: this.name_label.height,
        top: this.name_label.top + 35,
        left: this.name_label.left - 10,
        width: 275,
        borderRadius:15
    }),
    setItems = function() {
        this.win.add(this.name);
        this.win.add(this.name_label);
        this.win.add(this.desc);
        this.win.add(this.desc_label);
        Ti.API.info("label:"+ this.name_label.height);
        return this.win.open({modal: true, animation: true});  
    }
}

}

затем назвал это так:

app.controller.home = function() {
        var home = app.view.home();
        home.setItems();

        home.butn.addEventListener("click", function(e){
            app.controller.newMatrix();
        });

        home.butn2.addEventListener("click", function (e) {
            matrix_table(tab);
        });

        home.butn3.addEventListener("click", function(e){
            newItem();
        });

        home.butn5.addEventListener("click", function (e) {
            item_table(); 
        });

}

Я сделал это, потому что увидел здесь предложение Titanium MVC, но я не понимаю, почему он возвращает анонимный объект. Я не могу получить доступ к таким свойствам, как name_label из объекта name. Я понял, что я должен сделать это вместо:

app.view.newMatrix = function() {
    this.matrix = Titanium.UI.createWindow({  
        title:'Add a New Matrix',
        backgroundColor:'stripped',
        navBarHidden: false
    }), 
    // navbar buttons
    this.cancel = Titanium.UI.createButton({
        title:'Cancel'
    }),

    this.save = Titanium.UI.createButton({
        title:'Save',
        style:Titanium.UI.iPhone.SystemButton.SAVE
    }),

    this.name_label = Titanium.UI.createLabel({
        text: "Matrix Name:",
        font: { fontsize: 12, fontstyle: 'italic', color: '#336699' },
        height: 35,
        top: 35,
        left: 30,
        width: 150,
        color: "black"
    }),

    this.name = Titanium.UI.createTextArea({
        color: '#336699',
        height: this.name_label.height,
        top: this.name_label.top + 35,
        left: this.name_label.left - 10,
        width: 275,
        borderRadius:15
    }),

    this.setItems = function() {
        this.win.add(this.name);
        this.win.add(this.name_label);
        this.win.add(this.desc);
        this.win.add(this.desc_label);
        Ti.API.info("label:"+ this.name_label.height);
        return this.win.open({modal: true, animation: true});  
    }

}

и назовите это так:

app.controller.home = function() {
return {
    getView: function() {

        var home = app.view.home();
        home.setItems();

        home.butn.addEventListener("click", function(e){
            app.controller.newMatrix();
        });

        home.butn2.addEventListener("click", function (e) {
            matrix_table(tab);
        });

        home.butn3.addEventListener("click", function(e){
            newItem();
        });

        home.butn5.addEventListener("click", function (e) {
            item_table(); 
        });
    }
}

}

Но я не знаю, почему первый пример не работает. Я имею в виду, не являются ли свойства такими же, как переменные? Также я знаю, что второй пример - это тоже объект ... это то, как я должен их делать? Также во втором примере ключевое слово new является необязательным? Должен ли я использовать это? Я как бы хотел избежать этого, потому что не уверен, когда мне следует его использовать.

спасибо, надеюсь, я понял. У меня это работает, но я не знаю, является ли второй пример верным путем ...

...