处理meteor.call行为

时间:2016-05-10 11:08:32

标签: meteor meteor-accounts

这是一个疑问而非实际问题,但作为一名编程学生,我对此感到好奇: 在我的项目中,我有这个“文本输入到文本”元素,它允许快速编辑用户配置文件:

模板:

<template name="profile">
  <h3>Your Profile</h3>

  <div>
    {{#with userProfile}}
        <p><span>Name: </span> <span class="editableContentSolid" id="editableProfileName"> {{username}}{{#unless username}}No name!!!{{/unless}}</span></p>
        <p><span>eMail: </span> <span>{{emails.[0].address}}</span></p>
    {{/with}}
  </div>
</template>

有了这些事件:

Template.profile.events({
    'click .editableContentSolid' : function(event) {
            var html = $(event.target).text().trim();
            var editableText = 
                $("<input class='editableContentFluid' id='editableProfileName' type='text' placeholder='"+html+"'>");
            editableText.val(html);
            $(event.target).replaceWith(editableText);

            $("#editableProfileName").val(html);
            editableText.focus();
    },
    'blur .editableContentFluid' : function(event) {        
        var html = $(event.target).val();       
        var viewableText = $("<span class='editableContentSolid' id='editableProfileName'></span>");

        if (html == "") {
            viewableText.html(Meteor.user().username);
            $(event.target).replaceWith(viewableText);
        } else {
            viewableText.html(html);
            Meteor.call("isUser",html, function(error,result) {
                if(error) {
                    Bert.alert("Name already in use","warning");
                    viewableText.html(Meteor.user().username);
                    $(event.target).replaceWith(viewableText);
                } else {
                    Meteor.users.update({_id:Meteor.user()._id}, {$set:{username:html}});
                    $(event.target).replaceWith(viewableText);
                }
            });
        }
    }
});

最后方法调用:

isUser : function(nameOrMail) {
        if (! Meteor.userId()) {
            throw new Meteor.Error("not-authorized");
        }
        check(nameOrMail, String);

        var member = Accounts.findUserByUsername(nameOrMail);
        if (!member) {
            member = Accounts.findUserByEmail(nameOrMail);
        }

        if (!member) {
            return true;
        } else if(member) {
            throw new Meteor.error("user-already-exists");
        }
    }

它工作正常,客户端没有显示错误,但是当我输入现有用户名时,流星控制台会抛出错误:https://i.imgur.com/tVXaLs4.png

此外,有没有更好的方法来处理流星调用的错误/成功的东西?成功回归是否正确?

0 个答案:

没有答案