组合和扩展Racive组件
Combining and extending Ractive components
我开始使用Racive,我想更好地了解如何制作可重复使用的组件。编写一个可以在不同情况下使用的通用小部件非常容易。我的问题是,我的应用程序中的数据通常不是小部件所期望的形状,我不知道如何扩展小部件以使其更特定于应用程序。
举一个具体的例子,比如说我正在写一个Racive组件来设计一个折线图。我会做一些类似的事情
var Chart = Ractive.extend({
template: chart,
data: {
points_to_path: function(points) {
// whatever
}
}
});
使用类似的模板
<svg>
{{# points_to_path(points) }}
<path d="{{ path }}" />
{{/ end of path }}
</svg>
该组件可以像一样使用
var chart = new Chart({
data: {
points: [[1, 2], [3, 4], [5, 6]]
}
});
现在假设我实际上需要使用这样一个组件。可能我的数据不是像上面的points
那样由两个组件组成的数组。也许我有更像的东西
[
{
date: 'aug 2011',
value: 12.51
},
{
date: 'sep 2011',
value: 12.38
},
...
]
因此,我想做的是制作一个更特定于应用程序的Chart
,它接受上面形式的数据。我看不到用Chart.extend
实现这一点的简单方法,因为对points
变量的引用是在chart.html
中硬编码的。
在像Backbone这样的框架中,视图是嵌套的,所以我可以将内部小部件封装到一个更大的视图中,并在外部视图中定义数据转换的逻辑。我不太确定从这里到Ractive该怎么走。也许我应该从一开始就用不同的方式设计这个小部件。
更普遍地说,我的问题是我可以用Racive编写小组件,但我不太确定如何将它们组合成更大的组件。
Racive中专门化或扩展组件的惯用方法是什么?或者将许多组件组合成一个更大的应用程序?
有几种可能的解决方案。我不确定什么才是惯用语——一般来说,Racive的哲学是"如果它有效,那就是正确的解决方案!"-我是作者,但随着时间的推移,我会让社区来决定什么是惯用语。
1.初始化时传入函数
如果你使用d3来创建散点图,通常你会做这样的事情:
d3.select( 'circle' ).data( points )
.enter().append( 'circle' )
.attr( 'cx', function ( d ) { return d.x; })
.attr( 'cy', function ( d ) { return d.y; })
.attr( 'r', 5 );
换句话说,对于数组中的每个数据项,我们提供了一个函数,该函数提取绘制其x和y位置所需的值。
Racive等效方法是在初始化组件时指定一个功能:
<!-- the template -->
<svg>
<path d='{{ points_to_path(points) }}'/>
</svg>
// let's create a chart component that assumes we get an array
// of points with an `x` and a `y` property
var Chart = Ractive.extend({
template: chart,
data: {
points_to_path: function(points) {
// is there a 'normalise' function? If so, normalise
// the data we've been given
if ( var normalise = this.get( 'normalise' ) ) {
points = points.map( normalise );
}
// turn a normalised `{x: x, y: y}` point into 'x,y'
var pointToString = function ( point ) {
return point.x + ',' + point.y;
};
// create the SVG path
return 'M' + points.map( pointToString ).join( ' ' );
}
}
});
// now, we need to create a function that tells the Chart
// component how to deal with the data we're about to give it
var chart = new Chart({
data: {
points: [[1, 2], [3, 4], [5, 6]],
normalise: function ( point ) {
return { x: point[0], y: point[1] };
}
}
});
因此,如果你有一个具有date
和value
属性的点数组,你只需要传递一个normalise
函数(或者normalize
,如果你喜欢美式拼写…),它会返回{x: point.date, y: point.value}
或其他什么。
(当然,如果更容易的话,您可以覆盖整个points_to_path
函数。)
2.使用分部
另一种选择是在不同的情况下使用不同的部分:
<!-- widget template -->
<h2>This is a widget</h2>
<div class='contains-dynamic-contents'>
{{>inner}}
</div>
var widget = new Widget({
el: 'widgetContainer',
partials: {
inner: '<p>This paragraph will appear inside the div</p>'
}
});
这给了你很大的自由度,但折线图的例子可能更难适应。
内联组件
第一种方法也适用于内联组件——本例假设您使用的是即将发布的开发(0.3.8-pre)版本:
Ractive.components.linechart = Chart;
<div class='application'>
<h1>This is a chart</h1>
<linechart points='{{someData}}' normalise='{{someNormaliseFunction}}'/>
</div>
(Protip:您可以使用Chart.data.normalise = someFunction
指定默认的normalise
功能。)
第二种方法有点棘手——你必须在初始化时动态切换出部分:
Chart = Ractive.extend({
beforeInit: function () {
this.partials.inner = ( someCondition ? thisPartial : thatPartial );
},
// ...
});
很抱歉这个回复太长了,希望你想要的答案就在这里的某个地方!
- React重新渲染但未显示正确的组件
- 不能从angular2中的子组件指定父组件中的数组
- 拨打'父亲'函数形式a'儿童'ReactJS中的组件
- React组件等待所需道具进行渲染
- 如何创建带有插槽的vue js组件预加载程序
- KnockoutJS-组件-多个实例
- 如何更改reactjs中外部/独立组件的状态或属性
- 如何在react js中将值从一个组件发送到另一个组件
- React Native通过Navigator将用户输入数据传递到选项卡栏IOS中的组件
- React组件-设置页面标题
- 我的shareService在angular 2中发送值工作正常,但当我渲染我的另一个组件时,会获得默认值
- 如何在salesforce aura闪电组件中使用jQuery.val()
- 为react组件传递道具的最佳方式
- [Vue warn]:未能解析组件
- 如何控制组件'的createContent函数被激发
- react组件中的绑定方法
- 如何在react js中移动第二个组件
- 多个组件是如何实现的
- 组合和扩展Racive组件
- 多个Racive组件