我一直在寻找使用Dagger 2的MVP的Google Android架构示例:
但是,这个例子相当简单 - 每个Activity只有一个Fragment,Dagger Component在Activity中构建,并用于将片段的Presenter注入Activity.
我尝试构建该示例,将多个片段添加到Activity并在它们之间导航.由于每个片段都有自己的演示者,我已将匕首组件构建移动到片段中.所以现在我有:
活动做得很少,只是加载第一个片段并实现FragmentCallback,视图可以用来切换到另一个片段.
第一个片段有一个按钮,它使用FragmentCallback加载第二个片段 - 片段通过转换Activity来获得
public void onAttach(Context context) {
super.onAttach(context);
callback = (FragmentCallback) context;
}
Run Code Online (Sandbox Code Playgroud)
我在这里有明智的轨道吗?虽然使用MVP代码看起来很干净但我遗漏了匕首组件和模块的东西吗?
谢谢.
我通过为Activity创建一个Component和Module来改善我的状况.每个Fragment仍构建Dagger上下文,但我不再在presenter构造函数中注入视图(片段) - 当片段构建上下文时,注入自身(因此它有演示者)然后调用它presenter.init(this)
以便演示者现在具有风景.
这很好地减少了类的数量,下一步是尝试仅在活动中构建组件,并让片段使用它来注入自身(无需构建新组件).
如果我有模特用户:
var User = sequelize.define('User', {
name: {
type: Sequelize.STRING,
allowNull: false,
validate: {
notEmpty: {
msg: 'not empty'
}
}
},
nickname: {
type: Sequelize.STRING
}
});
Run Code Online (Sandbox Code Playgroud)
如何在name为null或未提供时指定消息?
这段代码:
User.create({}).complete(function (err, user) {
console.log(err);
console.log(user);
});
Run Code Online (Sandbox Code Playgroud)
生产:
{ [SequelizeValidationError: Validation error]
name: 'SequelizeValidationError',
message: 'Validation error',
errors:
[ { message: 'name cannot be null',
type: 'notNull Violation',
path: 'name',
value: null } ] }
Run Code Online (Sandbox Code Playgroud)
生成消息'name not not null'并且似乎不在我的控制之下.
使用User.create({name:''})向我显示我的自定义消息'not empty':
{ [SequelizeValidationError: Validation error]
name: 'SequelizeValidationError',
message: 'Validation error', …
Run Code Online (Sandbox Code Playgroud) 我正在寻找有关如何使用mongodb/mongoose
.
我目前尝试过:
userSchema.statics.findByFacebookIdOrCreate = function(facebookId, name, email) {
var self = this;
return this.findOne({
facebookId: facebookId
}).exec().then(function(user) {
if (!user) {
return self.model.create({
facebookId: facebookId,
name: name,
email: email
}).exec().then(function(user) {
return user;
});
}
return user;
});
};
Run Code Online (Sandbox Code Playgroud)
我从我的(节点/快递)API 端点调用它:
User.model.findByFacebookIdOrCreate(fbRes.id, fbRes.name, fbRes.email)
.then(function(user) {
return res.sendStatus(200).send(createTokenForUser(user));
}, function(err) {
return res.sendStatus(500).send({
error: err
});
});
Run Code Online (Sandbox Code Playgroud)
问题是:
谁能看到我做错了什么,以及我怎样才能做得更好?
谢谢。
更新
问题的原因是
self.model.create(...)
Run Code Online (Sandbox Code Playgroud)
应该是(没有模型参考)
self.create(...)
Run Code Online (Sandbox Code Playgroud)
但是,我现在需要知道我在错误处理方面做错了什么 - 我可以看到错误正在发生,但我看不到原因。
我仍然有一些错误发生,我知道这是因为我的状态为 …
android ×1
dagger-2 ×1
javascript ×1
mongodb ×1
mongoose ×1
mvp ×1
node.js ×1
promise ×1
sequelize.js ×1
validation ×1