- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
什么情况下会发生这种情况:
this.$el.append(ret); //doesn't work
$(this.el).append(ret); //works
我像这样实例化 View :
new allViews.UserProfile({el: '#main-content-id',collection: collections.users}),
所以也许我应该使用 $el 而不是 el 来实例化 View 。为什么 Backbone 会做出这样的区分,这太疯狂了。
我之前就应该提到这一点,但这是我正在做的一件非标准的事情。让我恼火的一件事是 Backbone 不允许 View 中使用“默认值”。这是错误的 - 例如 - 您可能需要 View 的默认集合,但如果您在构造函数中传递集合,那么它将覆盖默认值。相当合理。所以基本上我尝试手动将 Backbone.Model 中的内容复制为 Backbone.View。
所以这是 View 的全部内容,供您欣赏:
var UserProfileView = Backbone.View.extend({
defaults: function () {
return {
model: null,
collection: collections.users,
childViews: {}
}
},
events: {
},
constructor: function () {
this.givenName = '@UserProfileView';
Backbone.View.apply(this, arguments);
},
initialize: function (opts) {
Backbone.setViewProps(this, opts); //has side effects
},
render: function () {
var data = this.collection.models;
renderThis.bind(this)(UserProfileView.template);
function renderThis($template) {
var ret = EJS.render($template, {
users: data
});
//this.$el.append(ret);
$(this.el).append(ret);
}
return this;
}
},
{ //class properties
template: template
}
);
这是每个 View 都会运行的非标准函数,该函数在 view.initialize() 函数中调用:
Backbone.setViewProps = function(view,options){
var opts = options || {};
var temp = _.defaults({}, opts, _.result(view, 'defaults'));
for(var prop in temp){
if(temp.hasOwnProperty(prop) && prop !== undefined){
if(temp[prop]!==undefined){
view[prop] = temp[prop];
}
}
}
};
所以我认为正在发生的事情是,当我将 el 作为选项传递时,它会过早地附加到 View 上,然后 Backbone 过早地调用
this.setElement(_.result(this, 'el'));
来自 Backbone 源代码的内部
_ensureElement: function() {
if (!this.el) {
var attrs = _.extend({}, _.result(this, 'attributes'));
if (this.id) attrs.id = _.result(this, 'id');
if (this.className) attrs['class'] = _.result(this, 'className');
this.setElement(this._createElement(_.result(this, 'tagName')));
this._setAttributes(attrs);
} else {
this.setElement(_.result(this, 'el'));
}
},
依次调用
_setElement: function(el) {
this.$el = el instanceof Backbone.$ ? el : Backbone.$(el);
this.el = this.$el[0];
},
最佳答案
我完全不确定你想做什么,但我只会在评论中回答你的问题:
I think the best way someone could help me is to help create a better way to have default attributes for a View
和
One thing that irritates me is that Backbone doesn't allow for 'defaults' in views.
我不明白是什么阻止您在主干 View 上定义默认值并允许使用可以传入的选项参数进行覆盖:
var View = Backbone.View.extend({
initialize: function(options){
this.defaults = {
name: "default",
color: "blue"
};
this.attributes= $.extend({}, this.defaults, options);
}
})
您可以通过在实例化 View 时传入这些属性来覆盖它们:
var view = new View({name: "Alex", color: "red"});
关于Backbone view.render() 中的 jQuery 不规则性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31467844/
我需要在每个渲染帧完成后拍摄屏幕截图,但我发现某些屏幕截图是重复的,所以我想知道是否可以在渲染完成之前保存屏幕截图。因此... renderer.render() 会阻塞直到完成渲染吗? 如果没有,有
web.py 骨架代码中的“render._keywords['globals']['render'] = render”是什么意思? http://webpy.org/skeleton/0.3 最佳
所以在我的 Nuxt universal-mode 应用程序中,我有时会出现错误: vue.runtime.esm.js:620 [Vue warn]: The client-side rendere
我想创建一个 portal-like effect使用 Bevy . Unity 似乎有一个 render texture实现这一目标。 有没有办法在 Bevy 中做同样的事情?如果没有, futur
我有一个看起来像这样的组件(非常简化的版本): const component = (props: PropTypes) => { const [allResultsVisible, setA
编辑:我调整了代码,但问题仍然存在。见下文 我有这个 p:selectOneRadio : 而这个 p:radioButton : 和 AData包含其
为了渲染部分我可以使用 render 'partial_name' 或 render partial: 'partial_name' 我开始知道 render 是 render partial 的简写
我注意到文章中的一些地方使用了 React.render() 和一些地方 ReactDOM.render()。这两者有什么具体区别吗? 最佳答案 这是 0.14 中引入的最新更改。他们将 React
我的代码是这样的: function render() { renderer.render( scene, camera ); renderer.clear(); } 我想知道为什么它
我目前正在实现 useSWR 以便从我的 express 和 mongo-db 后端获取数据。我能够从数据库中成功获取数据没问题。以下是我用来实现此目的的代码: ```//SWR method for
我只有在按照 React native - "this.setState is not a function" trying to animate background color? 的建议合并了 u
所以我有一个大的纹理,被分成 64x64 block 。 我使用将其加载到 LibGDX texture = new Texture("texturemap.png"); regions = Text
我对放置 @Scripts.Render 和 @Styles.Render 的位置感到很困惑。理想情况下,我会将它们全部放在 head 部分中,但出乎意料的是,例如 @Scripts.Render("
我正在尝试使用 jamon 来收集使用 Tapestry 的网站的统计信息(呈现网页的时间)。 我怎样才能拥有 服务器收到请求时执行的方法,即渲染开始时? 响应全部发送完毕,即渲染结束时执行的方法 ?
在我的 React 应用程序中,我想要渲染一个 prop 值,但直到渲染完成后更新 props 后它才存在。 this.props.users 是一个对象,因此我使用 Object.keys() 转换
我正在使用 React 的钩子(Hook),我希望有一个从数据库中检索到的值作为初始值。但是,我收到以下错误: Invariant Violation: Invariant Violation: Re
我正在尝试按照以下代码将多个场景包含到单个 webgl 渲染器中: renderer.render(scene1, camera); renderer.render(scene2, camera);
我在我的 xhtml 页面中使用此代码,当我运行应用程序时,元描述仍在呈现。我想根据某些条件使用元描述标签。主布局: ..........
我正在使用react-native-render-html来渲染html。renderers方法允许我提供自定义函数来呈现特定标签。不过,我想使用源代码中的原始内部 HTML 将子组件替换为我的自定义
我有一个网格,可以渲染可变高度的卡片。 为了获取卡片的高度,我将卡片渲染在 ReactHeight 中。 ( https://github.com/nkbt/react-height ),这让我可以在
我是一名优秀的程序员,十分优秀!